Mvc C# Dropdown List Showing System.web.selectlistitem On The Model And Can Not Blind To Controller
Please help me. My dropdownlist showing System.Web.SelectListItem in debug mode but not showing actual text that I defined in the list. Can anyone please help. How make the control
Solution 1:
Follow below technique in which populate your SelectList from your controller. Its simple and clean:
Model
publicstring KeywordOptionsSelected { get; set; }
public SelectList KeywordOptions { get; set; }
Controller
model.KeywordOptions = newSelectList(new List<SelectListItem> {
new SelectListItem { Value = "TEST 1", Text = "Market Cap" },
new SelectListItem { Value = "TEST 2", Text = "Revenue" },
}, "Value", "Text");
View
@Html.DropDownListFor(model => model.KeywordOptionsSelected, Model.KeywordOptions, "--Select Option--", new { @id = "Dropdown_TEST" })
In this way, the code is easy to understand and View is also clean as all SelectList will be populated from cs.
You can make it more cleaner by populating SelectLists separately in methods and call in model.KeywordOptions
to populate it.
publicstatic List<SelectListItem> GetKeywords()
{
var keyword = new List<SelectListItem>();
keyword.Add(new SelectListItem { Value = "TEST 1", Text = "Market Cap" });
keyword.Add(new SelectListItem { Value = "TEST 2", Text = "Revenue" });
return keyword;
}
model.KeywordOptions = new SelectList(GetKeywords(), "Value", "Text");
Post a Comment for "Mvc C# Dropdown List Showing System.web.selectlistitem On The Model And Can Not Blind To Controller"