0
@foreach (var item in b)
{
    itemCount++;
    <input type="hidden" name="class@(itemCount.ToString())" value="@item.CouseClassId" />
    <input type="hidden" name="item@(itemCount.ToString())" value="@item.AnotherId" />
}

and the html will be like:

    <input type="hidden" name="class1" value="123" />
    <input type="hidden" name="item1" value="456" />
    <input type="hidden" name="class2" value="789" />
    <input type="hidden" name="item2" value="1011" />

My Controller:

[HttpPost]
public ActionResult CarAddStudent(object model) {
    return View("Another");
}

In the method's controller, how did I declare the model type and I will receive the dynamic value from Razor?

Steven Chou
  • 1,504
  • 2
  • 21
  • 43
  • 1
    Your model property names MUSTmatch the names of html input controls, or if you want to have controls with the same name then model takes that with an array matching that name – Ermir Beqiraj Dec 16 '16 at 08:00
  • Refer also [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Dec 16 '16 at 08:35

1 Answers1

5

you can use a list to receive them like this

<input type="hidden" name="class[0]" value="123" />
<input type="hidden" name="item[0]" value="456" />
<input type="hidden" name="class[1]" value="789" />
<input type="hidden" name="item[1]" value="1011" />

and your controller:

[HttpPost]
public ActionResult CarAddStudent(List<int> class,List<int> item) {
    return View("Another");
}

by the way, I guess that "class" and "item" are relational, so you may need to use a model list to receive,like this:

<input type="hidden" name="student[0].class" value="123" />
<input type="hidden" name="student[0].item" value="456" />
<input type="hidden" name="student[1].class" value="789" />
<input type="hidden" name="student[1].item" value="1011" />

and then , your receive model should be like this:

[HttpPost]
public ActionResult CarAddStudent(List<Student> student) {
    return View("Another");
}

class and item are the property of the Student Class

king
  • 66
  • 3
  • 1
    You can drop the `student` prefix. It should be just `name="[0].class"` etc –  Dec 16 '16 at 08:31