0

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.

3 Answers 3

1

I'm pretty confused by your controller code because it looks like a POST and a GET together since you are passing a list to the controller action method and you've got a ModelState.IsValid call.

You don't need to return your custom type to your view. The easiest would be for you to populate SelectListItem directly from your data provider.

ViewModel

public class StatesEditViewModel
{
    [Required]
    public int SelectedState { get; set; }
    public IEnumerable<SelectListItem> States { get; set; }
}

controller actions

    [HttpGet]
    public ActionResult StatesEdit()
    {
        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.

        var model = new StatesEditViewModel { States = stateResource.statesList.Select(s => new SelectListItem { Text = s.StateName, Value = s.StateID }).ToList() };

        return View(model);
    }

    [HttpPost]
    public ActionResult StatesEdit(StatesEditViewModel editModel)
    {
        if (!ModelState.IsValid)
        {
            //repopulate your state list
            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.

            var model = new StatesEditViewModel { States = stateResource.statesList.Select(s => new SelectListItem { Text = s.StateName, Value = s.StateID }).ToList() };
            return View(model);
        }

        //Save your selected sate to somewhere
        //redirect back to your GET action
        return RedirectToAction("StatesEdit");
    }

and finally your view

@model StatesEditViewModel

@{
    ViewBag.Title = "title";
    Layout = "_Layout";
}

<h2>title</h2>
@using (Html.BeginForm("StatesEdit", "<controller where you define you actions>", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.DropDownListFor(model => model.SelectedState, Model.States)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yep, you can bind DropDownListFor With Model. Using this:

@Html.DropDownListFor(m => m.StateID, new SelectList(Model.statesList ,"StateID", "StateName"))

public class StateResources
    {
        public List<States> statesList { get; set; }
        public int StateId { get; set; }
    }

In your view:

@model StateResources

In your controller:

public ActionResult StatesView(StateResources model)
{
  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.

         model.statesList = stateResource.statesList;         
     }
     catch (Exception ex)
     {
        // catches exception if exists
     }
  }
  else
  {
    //throws error if ModelState.IsValid == false.
  }

     return View("StatesView", model);
}

Comments

0

In View

<%=Html.DropDownList("TypeDropDown", Model.TypeSelectList, new { onclick = "Type_OnChange()" })%>

In View Model.

public SelectList TypeSelectList;

public void loaddata
{
 TypeSelectList = GetOptions();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.