Reputation: 1401
I have a class called Partner, that I need to Submit all the info from the View.
The Drop Down list needs to be filled by a dataset and on submit I want to fill SelectedHeardAboutText with the selected drop down item.
Here is my class:
public class Partner
{
public string Name { get; set; }
public string SelectedHeardAboutText { get; set; }
public IEnumerable<SelectListItem> HowDidYouHear { get; set; }
}
Here is my PartnerController:
public ActionResult Partner() {
var hear = db.HowDidYouHears.ToList();
var partner = new Partner();
ViewBag.Hear = hear;
return View(partner);
}
How would I go about creating my drop down on the view, my view model is bassed on the Partner class?
Upvotes: 0
Views: 5798
Reputation: 517
<http://dotnetpools.com/Article/ArticleDetiail/?articleId=48&title=Binding%20Dropdownlist%20In%20MVC3%20Using%20C#t;>
## razor ##
@Html.DropDownList("Country", new SelectList(Model.CountryList, "Value", "Text", Model.CountryList.SelectedValue), new { @id = "ddlist", @data_role = "none", style = "color: #fff; background-color: #fff; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: #333333; margin-left:3px; width:100%; height:20px;" })
## model ##
public class somename
{
public SelectList CountryList { get; set; }
}
public class Country
{
public string ID { get; set; }
public string Name { get; set; }
}
## Controller ##
public ActionResult index()
{
List<Country> objcountry = new List<Country>();
objcountry = GetCountryList();
SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
model.CountryList = objlistofcountrytobind;
return View(model);
}
[HttpPost]
public ActionResult Analyze(AnalyzeModels model)
{
List<Country> objcountry = new List<Country>();
objcountry = GetCountryList();
SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
model.CountryList = objlistofcountrytobind;
return View(model);
}
**************************************************
## function for GetCountryList##
public List<Country> GetCountryList()
{
DataTable reportDetailsTable = objCommon.GetDetails("tablename");
List<Country> objcountrty = new List<Country>();
int s = reportDetailsTable.Rows.Count;
for (int i = 0; i < s; i++)
{
string d1 = reportDetailsTable.Rows[i][1].ToString();
string d2 = reportDetailsTable.Rows[i][10].ToString();
objcountrty.Add(new Country { ID = d1, LName = d2 });
}
return objcountrty;
}
Upvotes: 0
Reputation: 1038830
@Html.DropDownList(
"SelectedHeardAboutText",
new SelectList((IEnumerable<Hear>)ViewBag.Hear, "Value", "Text")
)
Obviously that's ugly and I don't recommend it. Don't even know why I am posting it. Probably to say that it should never be used.
Here's the correct way to do it (by using strongly typed view):
public ActionResult Partner()
{
var partner = new Partner
{
HowDidYouHear = db.HowDidYouHears.Select(x => new SelectListItem
{
Value = x.Id, // this will be used as value
Text = x.SomeTextProperty // this will be used as text
})
};
return View(partner);
}
and in your view:
@Html.DropDownListFor(
x => x.SelectedHeardAboutText,
new SelectList(Model.Hear, "Value", "Text")
)
Upvotes: 1