I am fetching data from a SQL database table into an ASP.NET MVC Razor view but I have troubles getting changed input data into an Ajax post such as:
@model MyViewModel
<div class="row">
<div class="col-1">
@Html.LabelFor(model => model.Id, "Id:", new {})
</div>
<div class="col-4">
@Html.Label(Model.Id.ToString(), new { title = "" })
</div>
</div>
<div class="row">
<div class="col-1">
@Html.LabelFor(model => model.Test, "Test:", new { })
</div>
<div class="col-9">
@Html.TextBoxFor(model => model.Test, new { data_bind = "value: Test", @type = "text" })
</div>
</div>
@section scripts {
<script type="text/javascript">
$("#save-click").click(function () {
var nr = @Model.Id;
var postData = @Html.Raw(Json.Encode(@Model));
//alert(postData.Test);
$.ajax({
type: "POST",
url: actions.test.createOrUpdate + "?id=" + nr,
dataType: "json",
traditional: true,
data: postData,
success: function (response) {
if (response.Code == 0) {
else {
window.location.reload(false);
}
} else {
alert('err');
}
}
});
});
});
</script>
}
When I load the view everything is displayed correctly. The controller action is triggered correctly and the Id (which can not be altered) is passed properly too. However when input fields are changed not the changed values get passed to the controller but the original values that were fetched into the view.
The serialization seems to work, since the alert (postData.Test
) returns a value - but always the unchanged one.
Any help would be appreciated.