In my ASP.NET MVC 3 application, I am implementing an Edit view for my data entity. I am using a custom EditModel class to bind to, but this is unrelated to my question.

I am using an Awesome.MVC html helpers and the problem is tightly related to it.

As you can imagine, I have some data in place at the point I need to present it to a user for editing. But the data has a nested nature. For example, in my register view I had a radio list (ProcessId's) of string values. Until one of them isn't chosen, a dropdownlist (of ServiceTypes) isn't populated at all, since the helper has radio list set as it's parent:

@Html.LrRadioListFor(model => model.ProcessId, controller: "Incidents", action: "Processes")

...

@Html.LrDropdownFor(model => model.ServiceTypeId, parentId: "ProcessId", controller: "Incidents", action: "ServiceTypes")

Let Lr...For helper not bother you, essentailly the helpers are just Awesome.MVC standard, respectively-named html helpers.

So the register view loads, and asynchronously retrieves a set of available ProcessId's from Incidents/Processes. Once it's value is set, LrDropDownFor generated javascript will query Incidents/ServiceTypes for available service type options that correspond to the selected ProcessId.

Then a user a service type and the data is saved.

Whenever I want to allow a user to edit a previously submitted data, I am facing the following problem:

My EditModel already contains a ProcessId, as well as an Id of the selected during registration ServiceType. I am using the same Razor layout elements for edit view, as I have used for register.

The problem is that the model/view binding happens along the following path (or at least I think so:

  1. An html for RadioList is generated, and the selected value is set.
  2. The "setting" of the radio list submits a FeedServiceTypesforProcess ajax call A, without a selected item, since it's a "dumb" submitter, that just thinks, that the user has set the selected radio item.
  3. An html for DropDown is generated. Since the partent's ProcessId is already avaialbe, an ajax call B for feedServiceTypesforProcess is made, but this time, since the model provides an TypeId , a selected item's Id is provided (this is a "smart" request).
  4. For some reason, my Incidents controller gets a "smart" request first, answered to it, providing a "Selected:true" property data for a corresponding element.
  5. Then, the dumb request for ServiceTypes arrives, that was originated not from the model binding, but from the fact, that the radio item was set, so it just queries the possible ServiceTypes, with no particular "selected" element request in the response.

This causes my DropDown to be in a correct state for a moment, but then it reacts to the automatic radio button selection, and erases the results, form a model-bound ServiceType request.

Sorry for my English, and a somewhat complicated explanation of the problem. If it isn't clear at all, let me know, I will work more on making the problem situation description.

share|improve this question

1 Answer

up vote 1 down vote accepted

See My blog Cascading DropDownList in ASP.Net MVC which does this. Also See my DDL tutorials http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

share|improve this answer
Rick, thanks for the links. I've downloaded an inspected the first one from your blog, and I assume, you would have the same problem I've described if you were to implement an "edit" view, where you had the country and the state pre-selected, and still needed to provide the facility to edit both in a cascade manner. The Chosen plugin is a blast! I've spent a few days cooking one of my own out of the FunnelWebBlog's tag editor. – Maxim V. Pavlov Feb 12 '12 at 23:04

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.