问题描述
我想回回来PartialView或操作方法回到阿贾克斯后的任何其他视图。我想显示的内容ParitalView从哪个途径及其可能的AJAX成功功能或一个jQuery模态弹出。
I am trying to return back PartialView or any other view from action method back to ajax post. I wanted to display the contents ParitalView as a Jquery Modal pop-up from ajax success function or whichever way its possible.
MyRegistrationView,上面有报名表格已经下文提到的形式AJAX调用后提交按钮。
'MyRegistrationView' with Registration Form on it has below mentioned ajax post call on form submit button.
$.ajax({
url: url, //http://localhost/MyRegistration/RegisterUser
type: 'POST',
dataType: 'json',
data: ko.toJSON(RegistrationInfoModel),
contentType: "application/json; charset=utf-8",
success: function (result) {
//Do something
},
error: function (request, status, error) {
//Do something
}
});
以上AJAX调用去我控制器名为MyRegistrationController,采用如下操作方法。
The above ajax call goes to my Controller named " MyRegistrationController" with the action method as below.
[HttpPost]
public JsonResult RegisterUser(RegistrationInfo model)
{
//Register User
....
if(successful)
{
return Json(new { data = PartialView("_ShowSuccessfulModalPartial") });
}
}
现在
- 我怎样才能回来_ShowSuccessfulModalPartial的内容
AJAX的成功的功能和显示,截至模态上弹出了
同样的注册页面。 - 如果我想返回/重定向到其他一些观点我怎么能做到这一点
如果我有JsonResult作为我ActionMethod的返回类型。 - 如何我可以从注册过程中发回的回ModalErrors
我的看法,并显示他们的ValidationSummary下。
(注意:如果我不使用JsonResult作为返回类型我得到阿贾克斯parseerror意外令牌≤)
(Note: If I don't use JsonResult as return type i get ajax 'parseerror' Unexpected token <)
推荐答案
您可以返回的局部视图,而不是一个JSON。
You can return a partial view instead of a json.
在你的主视图,您shoudl添加对话框HTML像这样(assumming你使用jQueryUI的):
In your main view you shoudl add the dialog html like this (assumming you're using jQueryUI):
<div id="dialog-confirm" title="Your title">
<div id="dialog-content"></div>
</div>
请确保您初始化对话框。
Make sure you initialize the dialog.
$(document).ready(function() {
$("#dialog-confirm").dialog();
});
在控制器可能需要返回一个局部视图:
In the controller you might need to return a partial view:
[HttpPost]
public virtual ActionResult RegisterUser(RegistrationInfo model)
{
var model = //Method to get a ViewModel for the partial view in case it's needed.
return PartialView("PartialViewName", model);
}
然后,当你做你的Ajax请求,你的局部视图追加到对话框,然后显示它。
Then when you do your ajax request, you append the partial view to the dialog and then show it.
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: ko.toJSON(RegistrationInfoModel),
contentType: "application/json; charset=utf-8",
success: function (result) {
$("#dialog-content").empty();
$("#dialog-content").html(result);
$("#dialog-confirm").dialog("open");
},
error: function (request, status, error) {
//Do something
}
});
希望这有助于。
Hope this helps.
这篇关于返回PartialView从JsonResult ActionMethod回到阿贾克斯后并显示PartialView作为一个模式弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!