views:

17

answers:

0

I'm trying to create a form which will be used by clients to place an order for variable amount of models. It's similar to example from Steve Sanderson's blog, but I'm not using any javascript - I just have multiple submits in my form.

Everything works fine, I can add as many items as I want, but when I delete an item always the last one from list gets removed. Here's some code:

Fragment from Create.aspx:

<% for (int i = 0; i < Model.Input.Models.Count; i++ ){%>
  <%: Html.EditorFor(m => m.Input.Models[i])%>
  <p><input type="submit" name="deleteModel#<%:i%>" value="Delete" /></p>
<%}%>

Interesting fragment from ModelCreateModel.ascx EditorTemplate:

<div class="editor-field">
    <%: Html.EditorFor(model => model.Name) %>
    <%: Html.DisplayFor(model => model.Name) %>
</div>

In my OrderCreateModel I have a property Input of type OrderCreateModel.InputModel so Model.Input.Models is just a list of ModelCreateModels

Now if I:

  1. Add 3 models using this form (with names AAA, BBB, CCC)

  2. Click a delete#0 submit to remove model AAA

As a result I'll get editors for models AAA and BBB, but displays for models BBB and CCC. It's just too crazy for me! I've checked it with fiddler and this is really how the response looks like - it's not my browser's fault. Also, after debugging my code I'm 100% sure that I'm removing BBB model from my Input.Models before passing it to View().

Any suggestions how can I fix this?

Note: I don't want to use javascript for this. It'll be added later to provide better user experience, but site has to work as well with javascript disabled.

Edit:

Controller's code:

[HttpPost]
[ActionName("Create")]
[MultipleSubmits(InputName = "deleteModel", Argument = "deleteModelNr")]
[Authorize(Roles = RoleHelper.Accountant + "," + RoleHelper.Admin)]
public ActionResult CreateDeleteModel(OrderCreateModel.InputModel input)
{
    var viewModel = new OrderCreateModel {Input = input};
    FillCreateModelWithMaps(viewModel, input.ClientId);

    var deleteModelNr = (int) ViewData["deleteModelNr"];

    if (input.Models.Count > deleteModelNr)
        input.Models.RemoveAt(deleteModelNr);

    return View(viewModel);
}

MutlipleSubmitsAttribute inherits from ActionNameSelectorAttribute and here it extracts deleteModelNr from submits name and puts it in ViewData.

FillCreateModelWithMaps just fills viewModel.ExistingMaps with data.