【发布时间】:2017-12-04 12:35:59
【问题描述】:
在下拉列表中,我正在从数据库中获取值,但它工作正常……我需要手动添加<option value='-1'>Root</option>,这在数据库中不存在。
<div class="col-lg-4">
<fieldset class="form-group">
<label class="form-label" for="exampleInput">Domain Name</label>
@Html.DropDownList("DomainID", null, "--- Select Domain Name ---", new { @class = "select2-arrow" })
@Html.ValidationMessageFor(model => Model.DomainID, null, new { @style = "color: red" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
<label class="form-label" for="exampleInput">Parent Module</label>
<select id="ParentModuleID" class="select2-arrow" name="ParentModuleID"></select>
@Html.ValidationMessageFor(model => Model.ParentModuleID, null, new { @style = "color: red" })
</fieldset>
</div>
jquery:
$("#DomainID").change(function () {
var id = $(this).val();
$("#ParentModuleID").empty();
$.get("ParentModule_Bind", { DomainID: id }, function (data) {
var v = "<option>--- Select Domain Name ---</option>";
$.each(data, function (i, v1) {
v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";
});
$("#ParentModuleID").html(v);
});
});
在上面的 jquery <option>--- Select Domain Name ---</option> 我需要用value of -1 添加root
public JsonResult ParentModule_Bind(string DomainID)
{
userType type = new userType();
DataSet ds = type.ParentModule_Bind(DomainID);
List<SelectListItem> statelist = new List<SelectListItem>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
statelist.Add(new SelectListItem { Text = dr["ModuleName"].ToString(), Value = dr["ModuleID"].ToString() });
}
return Json(statelist, JsonRequestBehavior.AllowGet);
}
【问题讨论】:
-
有很多方法可以做到这一点。我遇到了这个问题。最好的方法和最佳实践是在 Controller 中添加您的手动值。加油!!
-
下面我的答案工作正常..它是否正确@Floxy
标签: javascript jquery asp.net-mvc asp.net-mvc-4