I've come to various examples, but I don't think I can find what I'm exactly looking for.
I have a model that contains a class:
public class StateResources
{
public List<States> statesList { get; set;}
}
public partial class States
{
public int? StateID { get; set; }
public string StateName { get; set; }
}
I have code which can execute to get data from a database which populates the statesList. I call that in my controller so I can display it in my view.
public ActionResult StatesView(List<States> listofStates)
{
if(ModelState.IsValid)
{
try
{
StatesResources stateResource = new StatesResources();
stateResource.ReturnStateResources(); // Assume there is a method in
// my States Resource Class
// that executes code to
// retrieve data from a db.
// and turns it into a list.
listOfStates = stateResource.statesList;
return(listOfStates);
}
catch (Exception ex)
{
// catches exception if exists
}
}
else
{
//throws error if ModelState.IsValid == false.
}
return View("StatesView");
}
In my view:
@model List<States>
<body>
@Html.DropDownListFor(m => m.StateID) @*This is where I'm having trouble*@
</body>
I don't know how to get the model to that drop down list. I believe the issue lies where it must be a select list item, but I'm not quite sure because I've seen DDL's that do not use them from some examples I've seen. I know there are several examples of how to pass data into a DDL, but I don't think I've come across an example similar to mine on how it's being retrieved from a db and making it a list of a specific class type.