0

At first - sorry for my broken English. May be it is stupid question, but.... So, I have a model

public class AAAModel {
  public int I1 {get; set}
  public string S1 ...
  public Guid G1 ...
}

I have a controller

public ActionResult Create(AAAModel WhatToCreate){
  ...
}

And I have a proper view to enter the data... All was fine, but now I must reorganize view to create several form and give the user ability to dynamically create new objects (model) and send query to the server. The data, that I got from server cames as Ajax query and parsed to the table grid... User may view this data, correct it and may send it back. So, I make several forms wich I duplicate from initial view and change the ID and Name of the controls Originally it was like

@Html.TextBoxFor(model => model.I1)
@Html.TextBoxFor(model => model.S1)
@Html.TextBoxFor(model => model.G1)

And now it bacame like

<input id="I1_1" name="I1_1 ...>
<input id="S1_1" name="S1_1 ...>
<input id="G1_1" name="G1_1 ...>

in form 1;

<input id="I1_2" name="I1_2 ...>
<input id="S1_2" name="S1_2 ...>
<input id="G1_2" name="G1_2 ...>

in form 2, and so on... Stupid question, but how can I modify the controller or what I have to do to got these values?

P.S. These forms will send via ajax... and host-page will not reload.

2 Answers2

0

Have a look here: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ and here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

UPDATE

You can always use FormCollection to get the passed model values.

[HttpPost]
public ActionResult MyAction(FormCollection values) 
{
   string I1_2 = values["I1_2"];    
   //do something
}

If you need you can work with dynamic objects specifying @model dynamic but you don't need to as the default for views is to derive from WebViewPage<dynamic>.

Also look here if you need to pass the anonymous object https://stackoverflow.com/a/5670899/1241400

Community
  • 1
  • 1
Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • Thanks. I think about these methods. Worst that in my case I has no list. The user must submit only one form at a time, but wich one - we don't know. And we can't make the same "name" attribute inside input elements - the unobtrusive validations will broke otherwise. :( I start thinking that global redesign of the page is needed... – Сергей Никитин Oct 15 '12 at 21:02
0

I dont know if under stand your question correctly. And sure if your are doing this and its not working but why not just simply create multiple forms on the page . So when the user submits a certain form it will only submit the inputs in that form.

@using (Html.BeginForm("Method","Controller",FormMethod.Post,new {id="form1"}))
{
  @Html.TextBoxFor(model => model.I1)
  @Html.TextBoxFor(model => model.S1)
  @Html.TextBoxFor(model => model.G1)
  <input type="submit" name="submitButton" id=form1Submit />
}
@using (Html.BeginForm("Method","Controller",FormMethod.Post,new {id="form2"}))
{
  @Html.TextBoxFor(model => model.I1)
  @Html.TextBoxFor(model => model.S1)
  @Html.TextBoxFor(model => model.G1)
  <input type="submit" name="submitButton" id=form2Submit />
}
...

of course you are going to create this dynamically. I am sorry if i am just repeating your question

EDIT

If your issue is how to determine what set of data was submitted I would add an id field to the model and change it for every new form you create

keshav
  • 866
  • 11
  • 19
  • In the simplest way the Submit of one of the form will reload all the page... And one more: does the validation will work on these forms? – Сергей Никитин Oct 17 '12 at 20:45
  • you can make these Ajax forms ... `@using (Ajax.BeginForm("Index", "Home", new AjaxOptions{...}` as for validation i have never done this my self but i would asume it would work. – keshav Oct 17 '12 at 21:04