我已经用数据库表中的值填充了一个下拉列表。列表中填充了正确的表数据,但所有值在列表中都有ZERO索引。以下是要填写下拉列表的代码:
//Get
public ActionResult NewBooking()
{
var db = new VirtualTicketsDBEntities2();
IEnumerable<SelectListItem> items = db.Attractions
.ToList()
.Select(c => new SelectListItem
{
Value = c.A_ID.ToString(),
Text = c.Name
});
ViewBag.Attractions = items;
return View();
}
在下拉视图页面上:
<div class="editor-label">
@Html.LabelFor(model => model.Attraction)
</div>
<div class="editor-field">
@Html.DropDownList("Attractions")
</div>
例如,若表有3个值A、B和C。这些值出现在下拉列表中,但当我在POST请求函数中获得其选定的索引时,它总是返回ZERO。这是POST提交功能:
//Post
[HttpPost]
public ActionResult NewBooking(BookingView booking)
{
try
{
BookingManager bookingManagerObj = new BookingManager();
bookingManagerObj.Add(booking);
ViewBag.BookingSavedSucess = "Booking saved!";
return View("WelcomeConsumer","Home");
}
catch
{
return View(booking);
}
}
即使用户选择的索引项大于ZERO,booking.Attraction
也始终为ZERO。
有什么建议吗?
我想这是因为您要返回的是SelectListItems
的集合,而不是实际的SelectList
。试试类似的东西:
<div class="editor-field">
@Html.DropDownListFor(model => model.Attraction, new SelectList(ViewBag.Attractions, "Value", "Text");
最好不要使用ViewBag
,应该始终使用ViewModel
。
假设你有一个像这样的ViewModel
:
public class AttractionViewModel
{
public int AttractionId { get; set; }
public SelectList Attractions { get; set; }
}
并像这样修改您的视图-我想您已经有了一个表单,相关的位是@Html.DropDownListFor(...)
,如果您还没有将ViewModel包含在Views-web.config文件中,请确保您拥有ViewModel的完整命名空间:
@model AttractionViewModel
@using(Html.BeginForm("NewBooking", "ControllerName"))
{
<div class="editor-label">
@Html.LabelFor(model => model.AttractionId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.AttractionId, Model.Attractions)
</div>
<input type="submit" value="Submit">
}
并像这样修改您的HttpGet
:
//Get
public ActionResult NewBooking()
{
var db = new VirtualTicketsDBEntities2();
var items = db.Attractions.ToList();
var attractionIdDefault = 0;// default value if you have one
var vm = new AttractionViewModel {
AttractionId = attractionIdDefault,// set this if you have a default value
Attractions = new SelectList(items, "A_ID", "Name", attractionIdDefault)
}
return View(vm);
}
并创建一个CCD_ 10 CCD_
// Post
public ActionResult NewBooking(AttractionViewModel vm)
{
var attractionId = vm.AttractionId; // You have passed back your selected attraction Id.
return View();
}
那么它应该会起作用。
我知道你已经选择了答案,但这里有一种替代方法。当我开始使用ASP.NET MVC
时,我很难使用SelectListItem
,并找到了另一种填充下拉列表的方法。从那以后,我一直坚持这种方式。
我总是有一个视图模型绑定到我的视图。我从不通过域模型发送,总是通过视图模型发送。视图模型只是域模型的缩小版本,可以包含来自多个域模型的数据。
我已经对您的代码和提示进行了一些修改,但正如我所提到的,这只是您现有代码的一种替代方案。
你的域模型可能是这样的。试着给你的房产名称一些有意义的描述:
public class Attraction
{
public int Id { get; set; }
public string Name { get; set; }
}
您查看的模型可能看起来像这样:
public class BookingViewModel
{
public int AttractionId { get; set; }
public IEnumerable<Attraction> Attractions { get; set; }
// Add your other properties here
}
不要在控制器中使用数据访问方法,而是让服务层或存储库暴露此功能:
public class BookingController : Controller
{
private readonly IAttractionRepository attractionRepository;
public BookingController(IAttractionRepository attractionRepository)
{
this.attractionRepository = attractionRepository;
}
public ActionResult NewBooking()
{
BookingViewModel viewModel = new BookingViewModel
{
Attractions = attractionRepository.GetAll()
};
return View(viewModel);
}
[HttpPost]
public ActionResult NewBooking(BookingViewModel viewModel)
{
// Check for null viewModel
if (!ModelState.IsValid)
{
viewModel.Attractions = attractionRepository.GetAll();
return View(viewModel);
}
// Do whatever else you need to do here
}
}
然后你的视图会像这样填充你的下拉列表:
@model YourProject.ViewModels.Attractionss.BookingViewModel
@Html.DropDownListFor(
x => x.AttractionId,
new SelectList(Model.Attractions, "Id", "Name", Model.AttractionId),
"-- Select --"
)
@Html.ValidationMessageFor(x => x.AttractionId)
我希望这能有所帮助。