【问题标题】:how to add default option value from dropdown using mvc?如何使用 mvc 从下拉列表中添加默认选项值?
【发布时间】: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 &lt;option&gt;--- Select Domain Name ---&lt;/option&gt; 我需要用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


【解决方案1】:

你可以像这样在 jQuesry 中添加一行

v += "<option value='-1'>root</option>";

或者像这样在foreach上方的控制器中添加一行

statelist.Add(new SelectListItem { Text = "root", Value = "-1" });

达到效果。

【讨论】:

  • 在您的代码中仅显示根值但未显示数据库值
  • 将这一行(我建议)放在foreach 语句的上方,并保留代码中的所有内容。
【解决方案2】:

为什么用 jQuery 在 UI 中做这些?

我看到的一个大问题是您将列表数据的业务逻辑与 UI 操作混合在一起。

如果无论 DomainID 为何,ParentModel 列表始终需要此值,那么我会修改返回列表的方法

public JsonResult ParentModule_Bind(string DomainID)
{
    userType type = new userType();
    DataSet ds = type.ParentModule_Bind(DomainID);

    //change the following line

    var statelist = new List<SelectListItem> { new SelectListItem { Text = "Root", Value = "-1" };
    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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多