Reputation: 111
I have an edit page and I want to submit some changes to the server when I press a button and then change the div of a given part of the page called #main to another page so as to update it with the new information.
To do this I don't think I can use a submit button (correct me if I am wrong?) so I am using a jquery function to handle the click and changing of the selected part of the page.
Currently the object sent to the server is empty and not the modified contents.
Here is the view that has the issue:
@model Project.Models.ExampleModel
@using (Html.BeginForm())
{
<fieldset>
<legend>Edit Example: @Model.name</legend>
<br/>
@Html.LabelFor(c => c.name)
@Html.TextBoxFor(c => c.name)
<br/>
@Html.LabelFor(c => c.dropdown)
@Html.DropDownListFor(c => c.dropdown, Model.dropdowns)
<br />
<br />
<input type="button" data-edit-id="@Model.id" value="Submit Changes" />
</fieldset>
}
<script type="text/javascript">
$(document).ready(function () {
$('input[data-edit-id]').on('click', function () {
var id = $(this).attr('data-edit-id');
console.log(id);
$.post('Home/EditPage', '@Model');
$("#main").load("/Home/NewDivPage?id=" + id);
});
});
</script>
Any help is greatly appreciated
Upvotes: 0
Views: 210
Reputation: 39491
This is wrong
$.post('Home/EditPage', '@Model');
@Model
is evaluated at server side, and you get simple string on client side with that expression. You should dynamically serialize form
and send its contents to server
$.post('Home/EditPage', $('form').serialize());
Also, the last statement should be passed as callback to $.post, so it will be executed after post has finished loading
$.post('Home/EditPage', $('form').serialize(), function(){
$("#main").load("/Home/NewDivPage?id=" + id);
});
Upvotes: 1