为什么我从下拉列表中获得Id而不是文本

本文关键字:Id 文本 下拉列表 为什么 | 更新日期: 2023-09-27 18:04:42

我的home controller是这样的

public ActionResult Index()
{
    List<District> allDistrict = new List<District>();
    List<Tehsil> allTehsil = new List<Tehsil>();
    List<SubTehsil> allSubTehsil = new List<SubTehsil>();
    using (FarmerBDContext db = new FarmerBDContext())
    {
        allDistrict = db.Districts.ToList();
    }
    ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName");
    ViewBag.TehsilId = new SelectList(allTehsil, "TehsilId", "Tehsilname");
    ViewBag.SubTehsilId = new SelectList(allSubTehsil, "SubTehsilId", "SubTehsilName");
    return View();
}

My District, Tehsil and SubTehsil下拉框将从另一个Model类中填充。

区类

public int DistrictId { get; set; }
public string DistrictName { get; set; }
public virtual ICollection<Tehsil> tbTehsils { get; set; }

My Tehsil和Subtehsil类是相似的,但是它们之间有关系

这个模型类是强类型到我的表单Index.cshtml

public string DistrictName { get; set; }
public string TehsilName { get; set; }
public string SubTehsilName { get; set; }
.... // Other Fields

索引视图是这样的

<table>
    <tr>
        <td class="editor-label Label">
            @Html.LabelFor(model => model.DistrictName)
        </td>
        <td>
            @Html.DropDownList("DistrictName", (SelectList)@ViewBag.DistrictId, " -- Select District -- ", new { @class = "ddl"})
        </td>
    </tr>
    <tr>
        <td></td>
        <td class="editor-field">
            @Html.ValidationMessageFor(model => model.DistrictName)
        </td>
    </tr>
    <tr>
        <td class="editor-label Label">
            @Html.LabelFor(model => model.TehsilName)
        </td>
        <td>
            @Html.DropDownListFor(model => model.TehsilName, @ViewBag.TehsilId as SelectList, "Select Tehsil", new { @class = "ddl" })
        </td>
    </tr>
    <tr>
        <td></td>
        <td class="editor-field">
            @Html.ValidationMessageFor(model => model.TehsilName)
        </td>
    </tr>
    ....
</table>
<p>
    <input type="submit" value="Details" />
</p>

我在另一个像这样的控制器中访问它们,这是基于我的模型属性。

ViewBag.DistrictName = model.DistrictName;
ViewBag.TehsilName = model.TehsilName;
ViewBag.SubTehsilName = model.SubTehsilName;

我也尝试使用下拉列表

的名称访问下拉列表的文本
Request.Form["DistrictName"].ToString()

,甚至使用

FormCollection form
还有一点需要注意的是,当我使用简单的 时
<select>
<option id=1>Abc</option>
</select>

我得到的文本是Abc

为什么我从下拉列表中获得Id而不是文本

因为<select>标签返回其所选选项的value属性。

的用法
ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName");

表示您的生成选项的value属性等于DistrictDistrictId属性。如果您检查生成的html,您将看到(假设您的集合包含new District() { DistrictId = 1, DistrictName = "ABC" };new District() { DistrictId = 2, DistrictName = "XYZ" };)

<select name="DistrictName" ...>
  <option value="1">ABC</option>
  <option value="2">XYZ</option>
</select>

如果您希望将DistrictName属性的值绑定到您的模型,那么它需要为

ViewBag.DistrictId = new SelectList(allDistrict, "DistrictName", "DistrictName");

或者你也可以写

IEnumerable<string> allDistrict = db.Districts.Select(d => d.DistrictName);
ViewBag.DistrictId = new SelectList(allDistrict); // a more appropriate name might be ViewBag.DistrictList

旁注:html表格元素用于表格数据,而不是布局!