Reputation: 101
My home controller looks like this
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 dropdown are getting populated from another Model class.
District Class
public int DistrictId { get; set; }
public string DistrictName { get; set; }
public virtual ICollection<Tehsil> tbTehsils { get; set; }
My Tehsil And Subtehsil class are similar but have relationships b/w them
This model class is strongly typed to my form Index.cshtml
public string DistrictName { get; set; }
public string TehsilName { get; set; }
public string SubTehsilName { get; set; }
.... // Other Fields
And my Index view is like this
<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>
I am accessing them in another controller like this which is based on my model properties.
ViewBag.DistrictName = model.DistrictName;
ViewBag.TehsilName = model.TehsilName;
ViewBag.SubTehsilName = model.SubTehsilName;
I also tried accessing text of dropdown using the name of dropdownlist
Request.Form["DistrictName"].ToString()
and even used
FormCollection form
One more thing to note is that when i use simple
<select>
<option id=1>Abc</option>
</select>
I am getting the Text which is Abc
Upvotes: 0
Views: 2155
Reputation:
Because a <select>
tag posts back the value
attribute of its selected option. You usage of
ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName");
means that your generating options which have a value
attribute equal to the DistrictId
property of District
. If you inspect the html your generating you will see (assuming you collection contains new District() { DistrictId = 1, DistrictName = "ABC" };
and new District() { DistrictId = 2, DistrictName = "XYZ" };
)
<select name="DistrictName" ...>
<option value="1">ABC</option>
<option value="2">XYZ</option>
</select>
If you want the value of the DistrictName
property to be bound to your model, then it needs to be
ViewBag.DistrictId = new SelectList(allDistrict, "DistrictName", "DistrictName");
Alternatively you could do
IEnumerable<string> allDistrict = db.Districts.Select(d => d.DistrictName);
ViewBag.DistrictId = new SelectList(allDistrict); // a more appropriate name might be ViewBag.DistrictList
Side note: html table elements are for tabular data, not layout!
Upvotes: 5