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?

2
  • 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 Commented Dec 16, 2016 at 8:00
  • Refer also this answer Commented Dec 16, 2016 at 8:35

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can drop the student prefix. It should be just name="[0].class" etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.