I have a View that gets some bits of data via Action methods that return JSON data.
Depending on the combination of selected options, the user can fill some fields in a page.
What is the best way to pass the data back to a controller, in order to be saved?
- The fields that contain data vary on the options selected;
- I don’t have a ViewModel object with all fields bound to the View.
At the moment I have this:
@Ajax.BeginForm("MyAction", null, new AjaxOptions
{
}, new { @id = "SaveForm" } )
{
.....
@Html.RadioButton("SomeRadioButton", "bla", false, new { @id = "SomeRadioButton" })
.....
@Html.TextArea("SomeTextArea", new { @id = "SomeTextArea" })
.....
<a href="#" onclick="$('#SaveForm').submit();">Save</a>
}
How do I get all of those control values in the Action?
I can add something like:
public void MyAction(FormCollection form)
{
.........
}
But I don’t really like this option.
What’s the cleanest way to implement this?
Thanks in advance
You could define a view model:
and then have your controller action take this view model as argument and leave the default model binder do its job:
I would also take advantage of this view model in the view in order to use strongly typed versions of the helpers: