Reputation: 166
I am trying to update my model that is bound to my view based on a user action (selecting a value in a dropdownlistbox). I can get the model using this var viewModel = @Html.Raw(Json.Encode(Model));
I can access the view model and update the properties but when I submit the form my model binding doesn't have my changes. Not sure what I'm doing wrong.
Upvotes: 0
Views: 4419
Reputation: 1038830
One possibility is to use AJAX to send the updated object to the server:
<script type="text/javascript">
// we serialize the model as a javascript object
var viewModel = @Html.Raw(Json.Encode(Model));
// we do some updates
viewModel.Foo = 'bar';
// and we post the model back to the server:
$.ajax({
url: '@Url.Action("SomeAction", "SomeController")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(viewModel),
success: function(result) {
// do some processing based on the result
// returned by the controller action
}
});
</script>
Upvotes: 2