How can I pass values back from a text box or dropdownlist or any control from a razor view back to an mvc3 controller? These values are not strongly bound so I don't have a model to bind these values to?
1 Answer
This will work:
public ActionResult SubmitAction(FormCollection collection) {
string formValue = collection["formValue"];
}
Edit:
public ActionResult SubmitAction(ModelName model, string field1, int field2) {
}
5 Comments
Mariah
Hi, thanks for the reply! So "formvalue" is the name of the control on the form (i.e. the textbox/input id?)? Is there another way to do this by saving back to a ViewBag within razor then the Viewbag can be accessed via the HttpPost Public ActionResult MethodNameHere(ModelName model) ? ? ?
endyourif
Maybe I misunderstood the original question, you have some strongly bound fields and some not? If so, the above edit I think works too.
Mariah
Yes, that is correct...some values will be coming in from thew view as bound to controls and the model and other values will be coming in from textboxes and dropdownlists that are not strongly bound to a model/control.
Mariah
Great thanks! QQ: How can I get the values from razor into field1 and field2 (to get back and access it via the actionresult)?
endyourif
It's been a while since I did it that way so it's either the FormCollection again or if that doesn't work, you can manually assign them to ViewBag variables and update the input/select boxes to use the ViewBag variable. For the select list, you'll need to do an if comparison.