views:

300

answers:

1

Hi

I am doing MVC and have look-up values in drop down lists. When calling UpdateModel only values before the look-ups get updated and nothing afyer. I get no errors though.

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

ViewData["SiteMaintenanceId"] = from 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()));

I have the following in my view:

<%= Html.DropDownList("SiteOperatorId")%>

This seems to bind ok and allows me to get the selected value on edit of my drop down and create works.

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

+1  A: 

It seems there is very little response to this query so I'll try my hand at it.

From the text it's a little difficult to understand the problem/requirement but if I understand you correctly you are trying to get back a value from a dropdown right? If not let me know and I'll edit this for a better fit.

Assuming I am correct however;

To setup my dropdown list I do things a little different. I don't think it's important but thought I'd share it anyway.

I have a FormViewModel like this;

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

The in my ActionResult I have the following to provide the States;

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

I then simply return that to the view.

The view has a drop down like this;

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

So now I have my list of states. On a postback I want to get the selected state. So...

[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 within the CalendarItem object are filled in.

However if you are not getting your values you may want to try;

String state = collection["selectedState"];

Again, I'm unsure if this answers your query and if it doesn't please attach a comment to this answer and I'll adjust accordingly.

griegs