I have form with a lot of inputs:
<input type="text" id="SomeName1" name="SomeName1" />
<input type="text" id="SomeName2" name="SomeName2" />
etc...
All these inputs are part of my viewmodel:
public class MyViewModel
{
public string SomeName1 { get; set; }
public string SomeName2 { get; set; }
//etc...
}
I need to refresh all those inputs without refreshing whole page, so I need to use Ajax request. However the problem is that we need explicitly define viewmodel in Javascript and send it via jquery.ajax
, like this:
var viewModel =
{
SomeName1: $("#SomeName1").val(),
//etc...
}
var request = $.ajax({
url: '@Url.Action("UpdateAction", "SomeController")',
type: "POST",
cache: false,
data: viewModel,
datatype: "json"
})
request.done(function (data) {
$("#SomeName1").val(data.SomeName1);
//etc...
})
So, there is a lot of copy-paste code, which is hard to edit and support.
I don't want explicitly send data, like in example, from all inputs and then explicitly set new data to all that inputs.
What should I do with this?