我是MVC的新手。我想只在Account_Type = "D"
填写Dropdownlist。
这是我的 Edit.cshtml
<div class="form-group">
@Html.LabelFor(model => model.Account_Code, "Account_Code", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Account_Code", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Account_Code, "", new { @class = "text-danger" })
</div>
</div>
这是我的编辑控制器
public ActionResult Edit(int? id)
{
ViewBag.Account_Code = new SelectList(db.Chart_Of_Account, "Account_Code", "Account_Desc", student.Account_Code);
}
答案 0 :(得分:0)
使用.Where()
子句过滤数据
public ActionResult Edit(int? id)
{
var accountCodes = db.Chart_Of_Account.Where(a => a.Account_Type == "D");
ViewBag.AccountCodeList = new SelectList(accountCodes , "Account_Code", "Account_Desc");
}
注意我已经更改了ViewBag
属性的名称,并删除了SelectList
构造函数的第3个参数(绑定到属性时忽略了它)
您的观点应为
@Html.DropDownListFor(m => m.Account_Code, (SelectList)ViewBag.AccountCodeList)