Last active
August 29, 2015 14:06
-
-
Save xDaevax/f5e192c03f6a2bfc2cdb to your computer and use it in GitHub Desktop.
Example Ajax MVC posting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace AjaxMvcTest.Controllers { | |
public class HomeController : Controller { | |
// | |
// GET: /Home/ | |
public ActionResult Index() { | |
HomeModel model = new HomeModel(); | |
model.Header = "Default Header"; | |
model.Content = "People assume that time is a strict progression of cause to effect, but actually, from a non-linear, non-subjective viewpoint, it's more like a big ball of wibbly-wobbly... timey-wimey... stuff."; | |
return View(model); | |
} | |
[ValidateInput(false)] | |
public JsonResult SaveContent(UpdateDataModel updateRequest) { | |
//Do some stuff | |
HomeModel newModel = new HomeModel(); | |
newModel.Content = updateRequest.Content; | |
newModel.Header = updateRequest.Header; | |
return Json(newModel); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace AjaxMvcTest.Controllers | |
{ | |
public class OtherHomeController : Controller | |
{ | |
public ActionResult Index() { | |
HomeModel model = new HomeModel(); | |
model.Header = "Default Header"; | |
model.Content = "People assume that time is a strict progression of cause to effect, but actually, from a non-linear, non-subjective viewpoint, it's more like a big ball of wibbly-wobbly... timey-wimey... stuff."; | |
return View(model); | |
} | |
[ValidateInput(false)] | |
public PartialViewResult SaveContent(UpdateDataModel updateRequest) { | |
//Do some stuff | |
HomeModel newModel = new HomeModel(); | |
newModel.Content = updateRequest.Content; | |
newModel.Header = updateRequest.Header; | |
return PartialView("ContentForm", newModel); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeModel { | |
public string Header { get; set; } | |
public string Content { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class UpdateDataModel { | |
public string Content { get; set; } | |
public string Header { get; set; } | |
public DateTime TimeStamp { get; set; } | |
public int Id { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model HomeModel | |
@{ | |
ViewBag.Title = "Index"; | |
Layout = "~/Views/Shared/_Layout.cshtml"; | |
} | |
<h2>Index</h2> | |
<div id="1" class="click2edit">@Model.Header</div> | |
<br /> | |
<div id="2" class="click2edit">@Model.Content</div> | |
<br /> | |
Header: <input type="text" name="HeaderInput" id="HeaderInput" value="@Model.Header" /> | |
<br /> | |
Content: <textarea name="ContentInput" id="ContentInput">@Model.Content</textarea> | |
<br /> | |
<input type="button" id="saveFileBtn" value="Save" /> | |
<script type="text/javascript"> | |
$(function () { | |
$("#saveFileBtn").click(function (e) { | |
$.ajax({ | |
url: '/Home/SaveContent', | |
type: 'POST', | |
data: getFormData(), | |
dataType: 'json', | |
success: function (data) { | |
$("#1").text(data.Header); | |
$("#2").text(data.Content); | |
$("#HeaderInput").val(data.Header); | |
$("#ContentInput").val(data.Content); | |
}, | |
error: function () { | |
alert("error"); | |
} | |
}); | |
}); | |
}); | |
function getFormData() { | |
//Convert the form data to a javascript object | |
var dataToSubmit = { | |
Header: $("#HeaderInput").val(), | |
Content: $("#ContentInput").val(), | |
TimeStamp: new Date(), | |
Id: 1 | |
}; | |
return dataToSubmit; | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model HomeModel | |
<div id="1" class="click2edit">@Model.Header</div> | |
<br /> | |
<div id="2" class="click2edit">@Model.Content</div> | |
<br /> | |
Header: <input type="text" name="HeaderInput" id="HeaderInput" value="@Model.Header" /> | |
<br /> | |
Content: <textarea name="ContentInput" id="ContentInput">@Model.Content</textarea> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model HomeModel | |
@{ | |
ViewBag.Title = "Index"; | |
Layout = "~/Views/Shared/_Layout.cshtml"; | |
} | |
<h2>Index</h2> | |
<div id="formPlaceholder"> | |
@{Html.RenderPartial("ContentForm", Model);} | |
</div> | |
<br /> | |
<input type="button" id="saveFileBtn" value="Save" /> | |
<script type="text/javascript"> | |
$(function () { | |
$("#saveFileBtn").click(function (e) { | |
$.ajax({ | |
url: '/OtherHome/SaveContent', | |
type: 'POST', | |
data: getFormData(), | |
success: function (data) { | |
$("#formPlaceholder").html(data); | |
}, | |
error: function () { | |
alert("error"); | |
} | |
}); | |
}); | |
}); | |
function getFormData() { | |
//Convert the form data to a javascript object | |
var dataToSubmit = { | |
Header: $("#HeaderInput").val(), | |
Content: $("#ContentInput").val(), | |
TimeStamp: new Date(), | |
Id: 1 | |
}; | |
return dataToSubmit; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment