dropdownlistfor not set value while循环模型甚至传递SelectList构造函数中的值

时间:2014-07-25 07:40:01

标签: loops model dropdownlistfor

MyModels

public class ExpenseEntryItems 
{
  public List<ExpenseEntryModel> NewItemList { get; set; }
  public List<SelectListItem> PaymentMethodList { get; set; }
  public List<SelectListItem> ActionList { get; set; }
}

public class ExpenseEntryModel
{
    [Display(Name = "Payment Method")]
    public string PaymentMethod { get; set; }

    [Display(Name = "Action")]
    public string Action { get; set; }
}

MyView的

@model REMClient.Models.ExpenseEntryItems
@using (Html.BeginForm("SubmitForm", "Expenses", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmFileUpload" }))
{
  @foreach (var i=0; i<Model.NewItemList.Count; i++)
  {
     @Html.DropDownListFor(modelItem => modelItem.NewItemList[i].PaymentMethod, new SelectList(Model.PaymentMethodList, "Value", "Text", Model.NewItemList[i].PaymentMethod), new { @class = "obj grid", id = "ddlPayMethod_" + Model.NewItemList[i].ExpDetailId })

     @Html.DropDownListFor(modelItem => modelItem.NewItemList[i].Action, new SelectList(Model.ActionList, "Value", "Text", Model.NewItemList[i].Action), new { @class = "obj grid", id = "ddlAction_" + Model.NewItemList[i].ExpDetailId })
  }
}

在控制器中,我使用值填充内部模型列表。 在视图中,它加载列表的第一个值并设置为剩余的选定值 网格中的dropdownlistfor

但它正在显示视图中的值但是如果我按照以下方式实现下拉列表,则不会在发布后获取控制器中的列表值

@using (Html.BeginForm("SubmitForm", "Expenses", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmFileUpload" }))
{
   @foreach (var i=0; i<Model.NewItemList.Count; i++)
   {
      string payment = Model.NewItemList[i].PaymentMethod;
      string action = Model.NewItemList[i].Action;
      @Html.DropDownListFor(modelItem => payment , new SelectList(Model.PaymentMethodList, "Value", "Text", payment ), new { @class = "obj grid", id = "ddlPayMethod_" + Model.NewItemList[i].ExpDetailId })

      @Html.DropDownListFor(modelItem => action , new SelectList(Model.ActionList, "Value", "Text", action ), new { @class = "obj grid", id = "ddlAction_" + Model.NewItemList[i].ExpDetailId })
  }
}

请建议我正确的实施方式

0 个答案:

没有答案