I am really confused by looking at so many approaches the way JQuery and AJAX are used with MVC and there is no proper documentation I found.
I have a very common scenario in my applications where I have a Form which is a partial view user will submit which will be stored to database if its valid and if not than show user same form again and show an error on the page.
Whats simplest way to do this ? Can I achieve this using simple AJAXForm or I will have to use JSON and JQuery as well ?
<script type="text/javascript">
function successContactList() {
alert("Success.");
}
function failureContactList() {
alert("Error.");
}
</script>
<% using (Ajax.BeginForm("SaveMailingList", "Contacts", new AjaxOptions { OnFailure = "failureContactList", OnComplete = "successContactList", UpdateTargetId = "ContactArea" })) { %>
<div id="ContactArea">
<%: Html.EditorFor(Model => Model) %>
<input type="submit" value="Save" />
</div>
<% } %>
[HttpPost]
public ActionResult SaveMailingList(IEnumerable<ContactsInMailingListsViewModel> contactInMailing) {
var mailingList = contactInMailing.FirstOrDefault();
var contact = repository.GetContactById(mailingList.ContactId);
repository.UpdateMailingList(contactInMailing,contact);
contactInMailing.FirstOrDefault().EndDate = new DateTime(2077,1,1);
return PartialView(contactInMailing);
}
I see two problems with your question.