【发布时间】:2015-06-13 16:38:55
【问题描述】:
我在 MVC 应用程序中有一个表单。回发返回模型,其中填充了除下拉列表的值之外的所有值。这是我为下拉菜单写的代码,我到底哪里出错了?
型号:
public class ViewModel : Model
{
public Dictionary<int, string> ServiceFocusList { get; set; }
public int ServiceFocus { get; set; }
}
查看:
@using
(Html.BeginForm("SaveServiceRequest","ServiceRequest",FormMethod.Post))
{
var AddLotLink = Url.Action("SaveServiceRequest", "ServiceRequest");
var labelCol = "control-label col-md-4";
@Html.LabelFor(model => model.ServiceFocusList, new { @class = @labelCol })
@Html.ValidationMessageFor(model => model.ServiceFocusList)
@Html.DropDownListFor(model => model.ServiceFocusList ,new SelectList(Model.ServiceFocusList,"Key",
"Value", Model.ServiceFocus), "Select a Service Focus")
<input type="submit" id="AddLotBtn" value="Add A Lot" class="saveDraft btn btn-default" urllink="@AddLotLink" />
}
控制器:
public ActionResult SaveServiceRequest(ViewModel model, long? lotId)
{
//At this point both model.ServiceFocusList and model.ServiceFocus are null
ServiceRequestSupport.SaveServiceRequest(model);
RedirectToAction("CreateLotOffSR", "Lot", new {area = "Lot", model.ID});
}
【问题讨论】:
-
你的 ddl 正在填充?您不需要控制器上的 FormCollection 来收集发布在表单上的信息吗?
标签: c# asp.net asp.net-mvc razor html.dropdownlistfor