在asp.net mvc 4 Web应用程序中,我使用viewmodel将我的数据加载到页面,编辑部分数据并更新相关模型。
目前我遇到了@Html.DropDownList
,虽然下拉列表显示正常,但根据您选择的内容,它不会更改/更新。
知道为什么会发生这种情况以及我做错了什么?
以下是我的代码:
控制器:
public ActionResult RequestDetails(int id)
{
var details = from s in SystemDB.Events.ToList()
where s.BookingId == id
orderby s.EventId descending
select s;
List<RequestDetailsViewModel> result = new List<RequestDetailsViewModel>();
foreach (var item in details)
{
RequestDetailsViewModel model = new RequestDetailsViewModel();
model.Event.EventId = item.EventId;
model.Event.EventLeader = item.EventLeader;
model.Event.EventStartDateTime = item.EventStartDateTime;
model.Event.EventEndDateTime = item.EventEndDateTime;
model.Event.Room.RoomNumber = item.Room.RoomNumber;
model.Event.EventTotalCost = item.EventTotalCost;
model.Event.Booking.BookingId = item.BookingId;
result.Add(model);
}
Booking booking = SystemDB.Bookings.Find(id);
var statuscodes = from s in SystemDB.StatusCodes.ToList()
where s.StatusCodeId != 1
select s;
ViewBag.StatusCodeList = new SelectList(statuscodes, "StatusCodeId", "StatusCodeName", booking.StatusCodeId);
return View(result);
}
[HttpPost]
[ActionName("RequestDetails")]
public ActionResult PostRequestDetails(int id)
{
var details = from s in SystemDB.Events.ToList()
where s.BookingId == id
orderby s.EventId descending
select s;
List<RequestDetailsViewModel> result = new List<RequestDetailsViewModel>();
foreach (var item in details)
{
RequestDetailsViewModel model = new RequestDetailsViewModel();
model.Event.EventId = item.EventId;
model.Event.EventLeader = item.EventLeader;
model.Event.EventStartDateTime = item.EventStartDateTime;
model.Event.EventEndDateTime = item.EventEndDateTime;
model.Event.Room.RoomNumber = item.Room.RoomNumber;
model.Event.EventTotalCost = item.EventTotalCost;
model.Event.Booking.BookingId = item.BookingId;
result.Add(model);
}
Booking booking = SystemDB.Bookings.Find(id);
var statuscodes = from s in SystemDB.StatusCodes.ToList()
where s.StatusCodeId != 1
select s;
ViewBag.StatusCodeList = new SelectList(statuscodes, "StatusCodeId", "StatusCodeName", booking.StatusCodeId);
if (ModelState.IsValid)
{
SystemDB.Entry(booking).State = EntityState.Modified;
SystemDB.SaveChanges();
return RedirectToAction("RequestList", "Admin");
}
return View(result);
}
Viewmodel:
public class RequestDetailsViewModel
{
public RequestDetailsViewModel()
{
this.Event = new Event();
this.CateringItem = new CateringItem();
this.Item = new Item();
this.Event.Room = new Room();
this.Event.Booking = new Booking();
this.Event.Booking.StatusCode = new StatusCode();
}
public Event Event { get; set; }
public CateringItem CateringItem { get; set; }
public Item Item { get; set; }
}
查看:
@model IEnumerable<CateringBookingSystem.ViewModels.RequestDetailsViewModel>
@Html.ActionLink("Manage Requests", "RequestList")/<b>Event List</b>
@using (Html.BeginForm("RequestDetails", "Admin", FormMethod.Post))
{
<span><b>Booking for: </b>@ViewBag.Date</span>
<span style="padding-left:20px;"><b>Total Cost: </b>£@ViewBag.BookingTotalCost</span>
<span style="padding-left:20px;"><b>Requester: </b><a href="mailto:'@ViewBag.RequesterEmail'?Subject=Catering%20enquiry" target="_top">@ViewBag.Requester</a></span>
<span style="padding-left:20px;"><b>Status: </b> @Html.DropDownList("StatusCodeList") </span>
<table id="listTable" style="width:100%" >
<tr style="color:white;">
<th>@Html.DisplayNameFor(model => model.Event.EventLeader)</th>
<th>@Html.DisplayNameFor(model => model.Event.EventStartDateTime</th>
<th>@Html.DisplayNameFor(model => model.Event.EventEndDateTime</th>
<th>@Html.DisplayNameFor(model => model.Event.Room.RoomNumber)</th>
<th>@Html.DisplayNameFor(model => model.Event.EventTotalCost)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Event.EventLeader)</td>
<td>@Html.DisplayFor(modelItem => item.Event.EventStartDateTime)</td>
<td>@Html.DisplayFor(modelItem => item.Event.EventEndDateTime</td>
<td>@Html.DisplayFor(modelItem => item.Event.Room.RoomNumber)</td>
<td>£@Html.DisplayFor(modelItem => item.Event.EventTotalCost</td>
</tr>
}
</table>
<div style="padding-top:50px;">
<input type="submit" value="submit" />
</div>
}
<div style="padding-top:20px;">
<img src="~/Images/arrow-return-180-left.png" style="margin-bottom:-3px;margin-right:-4px;" /> @Html.ActionLink("Back to Requests", "RequestList")
</div>
答案 0 :(得分:0)
试试这个
@Html.DropDownList("StatusCodeList", (SelectList) ViewBag.StatusCodeList, "Select")
答案 1 :(得分:0)
您的ViewBag.StatusCodeList已经是SelectList的类型。我没有看到两次创建SelectList的原因,所以不应该只是:
@Html.DropDownList("StatusCodeList", ViewBag.StatusCodeList)
或
@Html.DropDownList("StatusCodeList", ViewBag.StatusCodeList as SelectList)
答案 2 :(得分:0)
你GET方法正在向视图返回RequestDetailsViewModel
的集合。要正确呈现集合以进行回发,请使用自定义EditorTemplate
作为RequestDetailsViewModel
类型,并将POST方法更改为
[HttpPost]
public ActionResult RequestDetails(IEnumerable<RequestDetailsViewModel> model)
注意,在大多数情况下,您可以使用for
循环在视图中呈现集合,但DropDownListFor()
中存在一个错误,阻止控件在{{1}中正确绑定} loop(尽管你绑定了属性的值,但总会选择第一个选项。)
编辑模板
/Views/Shared/EditorTemplates/RequestDetailsViewModel.cshtml
for
在主视图中,您需要将@model RequestDetailsViewModel
@Html.HiddenFor(m => m.Event.Booking.EventId)
@Html.TextBoxFor(m => m.Event.Booking.EventLeader)
....
@Html.DropDownListFor(m => m.Event.Booking.StatusCodeId, (SelectList)ViewData["StatusCodeList"], "-Please select-")
....
传递给SelectList
参数
additionalViewData
GET方法
@model IEnumerable<RequestDetailsViewModel>
@using(Html.BeginForm())
{
@Html.EditorFor(m => m, new { StatusCodeList = (SelectList)ViewBag.StatusCodeList })
<input type="submit" />
}