MVC - UpdateModel and DropDownLists

I am doing MVC and have dropdowns in dropdowns. When you call UpdateModel, the values ​​are only before the search queries are updated and nothing else is up to date. I am not getting any errors.

I can edit and create and use the following code in my cintroller:

ViewData ["SiteMaintenanceId"] = of m in this._siteRepository.FindAllSiteMaintenances (). ToList ()

     select new SelectListItem
     {
       Text = m.Maintenance,
       Value = m.SiteMaintenanceId.ToString(),
       Selected = (m.SiteMaintenanceId == site.SiteMaintenanceId)
     };


       return View(new SiteFormViewModel(site,               
       this._siteRepository.FindAllSiteOperators()));

      

In my opinion, I have the following:

 

It seems to be bound to the window and allows me to get the selected value while editing my dropdown and create works.

This is my first time doing MVC, so any help is greatly appreciated.

+2


source to share


1 answer


There seems to be very little answer to this query, so I'll try on it.

From the text, it's a bit tricky to understand the problem / requirement, but if I understand you correctly, are you trying to return the value from the dropdown on the right? If you don't let me know and I'll edit this for a better fit.

Assuming I'm right

To customize my dropdown I am doing something completely different. I don't think this is important, but I thought I would share it.

I have a FormViewModel like this:

public class CalendarEventFormViewModel
{
    public CalendarItem Event { get; set; }
    public SelectList States;
}

      

In my ActionResult, I have the following to provide states;

fvm.States = new SelectList(Enumerations.EnumToList<Enumerations.AustralianStates>(), "Value", "Key", fvm.Event.state);

      

Then I just put that back in the view.



The view looks like this:

<% using (Html.BeginForm()) { %>
  <%=Html.DropDownList("selectedState", Model.States, new { @class="stateSelector" })%>
<%} %>

      

So now I have a list of states. In postback, I want to get the selected state. So that...

[AcceptVerbs(HttpVerbs.Post), ValidateInput(false), Authorize]
public ActionResult Add(FormCollection collection)
{
    CalendarItem fvm = new CalendarItem();
    UpdateModel(fvm);
}

      

Now this works for me and all the fields inside the CalendarItem are populated.

However, if you are not getting your values, you can try:

String state = collection["selectedState"];

      

Again, I'm not sure if this answers your request, and if it doesn't attach a comment to this answer and I'll adjust it accordingly.

+1


source







All Articles