admin管理员组文章数量:1329105
I'm using MVC 4 and I am trying to send a post request and I am getting a successful return from my controller with the resulting view in HTML form, but I'm not sure what to do with it at that point.
JS
$("form").submit(function (e) {
e.preventDefault();
if ($(this).valid()) {
$.ajax({
url: submitUrl, type: "POST", traditional: true,
data: { EventName: 'Name of event'},
success: function(data){
$("#content").html(data);
}
})
}
});
my controller
[HttpPost]
public ActionResult CreateEvent(EventModel model)
{
if(ModelState.IsValid)
{
return RedirectToAction("Index");
}
else
{
return View(model);
}
}
So you can see that my controller either returns a View or a RedirectToAction. In the success callback of my ajax call I am doing the following: $("#content").html(data); But nothing seems to happen. Can someone help push me in the right direction of properly handling the return of the controller action.
Thank you so much!
I'm using MVC 4 and I am trying to send a post request and I am getting a successful return from my controller with the resulting view in HTML form, but I'm not sure what to do with it at that point.
JS
$("form").submit(function (e) {
e.preventDefault();
if ($(this).valid()) {
$.ajax({
url: submitUrl, type: "POST", traditional: true,
data: { EventName: 'Name of event'},
success: function(data){
$("#content").html(data);
}
})
}
});
my controller
[HttpPost]
public ActionResult CreateEvent(EventModel model)
{
if(ModelState.IsValid)
{
return RedirectToAction("Index");
}
else
{
return View(model);
}
}
So you can see that my controller either returns a View or a RedirectToAction. In the success callback of my ajax call I am doing the following: $("#content").html(data); But nothing seems to happen. Can someone help push me in the right direction of properly handling the return of the controller action.
Thank you so much!
Share Improve this question edited Apr 2, 2013 at 15:22 mvcNewbie asked Mar 30, 2013 at 2:17 mvcNewbiemvcNewbie 5201 gold badge11 silver badges23 bronze badges 11- You should return a JSON example: {"me": "its ok"} in your "success" should parse and display the message. – John Paul Cárdenas Commented Mar 30, 2013 at 2:21
- You can use the Window.location of javascript for redirect as you are doing in the server side. Return success flag to client and let him choose the path – Devesh Commented Mar 30, 2013 at 2:22
- Hi John, sorry, but I didn't quite understand your statement. Should I return Json(View(model)) from my controller and leave my Javascript how it is? Thanks. – mvcNewbie Commented Mar 30, 2013 at 2:23
- HI Devesh, I thought about that, but I might need to do server validation on the model and return that "View(model)", so I'd want to see the modified model. – mvcNewbie Commented Mar 30, 2013 at 2:24
- First you must define what you want to return, you can give an example of what is returned? – John Paul Cárdenas Commented Mar 30, 2013 at 2:24
1 Answer
Reset to default 4If I understand correctly, you have a Create Event form on your page and you want to send an AJAX request to create a new event. Then you want to replace a section in your page #content
with the results of the CreateEvent
action.
It looks like your AJAX is set up correctly so that CreateEvent
returns a successful response. I think you're now confused about the response. You have several options but let's choose two.
JSON response
[HttpPost]
public ActionResult CreateEvent(EventModel model)
{
if(ModelState.IsValid)
{
var event = service.CreateEvent(model); // however you create an event
return Json(event);
}
else
{
// model is not valid so return to original form
return View("Index", model);
}
...
Now you need to generate html markup from the JSON and insert it into #content
.
$.ajax({
url: submitUrl, type: "POST", traditional: true,
data: { EventName: 'Name of event'},
success: function(data){
var obj = JSON.Parse(data);
var html; // build html with the obj containing server result
$("#content").html(html);
}
})
or HTML fragment
Instead of returning a full page with a Layout
defined we'll return just a PartialView
without Layout
and all the head
and script
tags.
[HttpPost]
public ActionResult CreateEvent(EventModel model)
{
if(ModelState.IsValid)
{
var event = service.CreateEvent(model); // however you create an event
return PartialView("CreateEventResult", event);
}
else
{
// model is not valid so return to original form
return View("Index", model);
}
}
Now make a new partial view CreateEventResult.cshtml (Razor)
@model Namespace.EventModelResult
@ {
Layout = null;
}
<div>
<!-- this stuff inserted into #content -->
@Model.Date
...
</div>
and the javascript is unchanged
$.ajax({
url: submitUrl, type: "POST", traditional: true,
data: { EventName: 'Name of event'},
success: function(data){
$("#content").html(data);
}
})
Edit: If your Event creation fails for any reason after you have validated the input, you'll have to decide how you want to respond. One option is to add a response status to the object you return and just display that instead of the newly created Event.
本文标签: javascriptHow to properly render the return of AJAX POST MVC 4Stack Overflow
版权声明:本文标题:javascript - How to properly render the return of AJAX POST MVC 4 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742233461a2437645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论