0

我需要在页面加载时用一些静态列表项填充两个下拉列表,为此我已经这样做了......

控制器

  public ActionResult Index()
  {           
       GetStatus();
       GetChangeTypeTransactions();
       return View();
  }
  private static TransactionHistoryModel GetStatus()
  {
        var model = new TransactionHistoryModel
        {
            Status= "None",
            StatusList = new[]
            {
                 new SelectListItem { Value = "None", Text = "None" },
                 new SelectListItem { Value = "Success", Text = "Success" },
                 new SelectListItem { Value = "Failed", Text = "Failed" },
                 new SelectListItem { Value = "Queued", Text = "Queued" }
            }
        };
        return model;
    }

    private static TransactionHistoryModel GetChangeTypeTransactions()
    {
        var model = new TransactionHistoryModel
        {
            TypeOfChange = "None",
            TypeOfChangeList = new[]
            {
                new SelectListItem { Value = "None", Text = "None" },
                new SelectListItem { Text = "Delete Item", Value = "Delete Item" },
                new SelectListItem { Text = "Delete Vendor", Value = "Delete Vendor" },
                new SelectListItem { Text = "Delete Member", Value = "Delete Member" },
                new SelectListItem { Text = "Update CostPage Header", Value = "Update Cost Page Header" },
                new SelectListItem { Text = "Update Item", Value = "Update Item" }
            }
        };
        return model;                              
    }

看法

 <div class="firstTxtBox">
       @Html.DropDownListFor(m=>m.Status, Model.StatusList,new { style = "width:120px" })  
    </div>
    <div class="SecondLbl">
        @Html.Label("Type Of Change")
    </div>
     <div class="SecondTxtBox">
        @Html.DropDownListFor(m=>m.TypeOfChange, Model.TypeOfChangeList, new { style = "width:120px" })
     </div>

模型

public class TransactionHistoryModel
{
    public string Status { get; set; }
    public IEnumerable<SelectListItem> StatusList { get; set; }
    public string TypeOfChange { get; set; }
    public IEnumerable<SelectListItem> TypeOfChangeList { get; set; }
}

但我在这一行的开头得到了 NullReference 异常:

 @Html.DropDownListFor(m=>m.Status, Model.StatusList,new { style = "width:120px" })  

我需要在页面加载时将项目填写在两个下拉列表中,但我出现错误,任何人对此有任何想法和建议....我使用的是 MVC 4 版本

提前谢谢了...

4

1 回答 1

0

试试这个,

控制器

 public ActionResult Index()
  {     
          TransactionHistoryModel model = new TransactionHistoryModel();      
           model.StatusList = GetStatus();
            model.TypeOfChangeList = GetChangeTypeTransactions();
OR
        ViewBag.statusasdID = GetStatus();
        ViewBag.TypeofasdID = GetChangeTypeTransactions();
           return View(model);
      }

看法

@Html.DropDownListFor(m => m.statusid, Model.StatusList , "--Select--")

@Html.DropDownListFor(m => m.TypeofId, Model.TypeOfChangeList , "--Select--")

OR
   @Html.DropDownList("statusid", (SelectList)ViewBag.statusasdID , "--Select--")
   @Html.DropDownList("TypeofId", (SelectList)ViewBag.TypeofasdID , "--Select--")

模型

    public class TransactionHistoryModel
    {
        public int statusid { get; set; }
        public int TypeofId { get; set; }
        public string Status { get; set; }
        public IEnumerable<SelectListItem> StatusList { get; set; }
        public string TypeOfChange { get; set; }
        public IEnumerable<SelectListItem> TypeOfChangeList { get; set; }
    }
于 2013-08-27T09:45:03.490 回答