我需要填充两个下拉列表,并在页面加载中显示一些静态列表项目,为此我已经这样做了...
控制器
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 Exception:
@Html.DropDownListFor(m=>m.Status, Model.StatusList,new { style = "width:120px" })
我需要在页面上加载两个下拉列表中的项目加载它自己但是我得到错误,是否有任何想法和任何建议....我正在使用MVC 4版本
非常感谢提前......
答案 0 :(得分: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; }
}