[英]refresh Json Partial View
I would like to refresh Json Partial View. 我想刷新Json Partial View。 I am trying use this:
我正在尝试使用这个:
$('#example123').load('@Url.Action("Rejestracja", "Logowanie")');
but doesn't work correct. 但是不正确。
$.ajax({
url: '@Url.Action("Rejestracja", "Logowanie")',
dataType: "json",
type: "POST",
async: false,
error: function () {
},
success: function (data) {
if (data.Success) {
var el1 = $('<div><strong style="color: black" id="example123">' + data.View + '</strong></div>');
$(el1).dialog(
{
modal: true,
title: '<div></div>',
minWidth: 340,
minHeight: 300,
buttons: {
'Zamknij': function () {
$('#example123').load('@Url.Action("Rejestracja", "Logowanie")');
$(this).dialog("destroy");
}
}
});
}
}
});
JSON Partial View JSON部分视图
[HttpPost]
public JsonResult Rejestracja()
{
bool dataCahnged = true;
var model = new Logowanie();
object view = this.RenderPartialViewToString("Rejestracja", model);
return Json(new { Success = true, dataCahnged, View = view });
}
(this work only first time) (这项工作仅限第一次)
<script type="text/javascript">
$('#bladReje').hide();
$('#bladRejeText').hide();
$('#poprawnieReje').hide();
$('#poprawnieRejeText').hide();
</script>
Example (this work only first time) 示例(此工作仅限第一次)
<div id="ex"></div>
<script type="text/javascript">
$('#ex').text('sasasa');
</script>
Any reason in particular why you're calling the ajax using "POST"?, you're not sending any information to the server, so this is the first thing that I would change. 有什么理由特别说你为什么用“POST”调用ajax?你没有向服务器发送任何信息,所以这是我要改变的第一件事。 Try changing this to GET.
尝试将其更改为GET。
In second place, try to change your method to get your view html to return a string. 在第二位,尝试更改您的方法以使您的视图html返回一个字符串。 I'm using this extension method and it works just fine:
我正在使用这种扩展方法,它工作得很好:
public static class JsonHelper
{
public static string RenderPartialView(this Controller controller, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}
And here's an example about how to use it: 以下是有关如何使用它的示例:
public ActionResult GetClientList()
{
List<ClientModel> clientList = ClientRepository.GetClients();
return Json(new JsonMixedResult { Result = "success", ViewHtml = this.RenderPartialView("ClientList", clientList) }, JsonRequestBehavior.AllowGet);
}
Finally, check the final parameter on the return Json statement. 最后,检查返回Json语句的最后一个参数。 That allows your client code to access the returned data from you Controller.
这允许您的客户端代码从您的Controller访问返回的数据。 In this case, is a Json object that includes your view html.
在这种情况下,是一个包含您的视图html的Json对象。
Hope this helps 希望这可以帮助
I don't see why you just don't return PartialView??? 我不明白为什么你只是不返回PartialView ??? I mean your Json result returns your partial view with 2 additional properties indicating success and that data has changed I guess but those two look pointless to me.
我的意思是你的Json结果返回你的局部视图,有2个额外的属性表明成功,而且数据已经改变了,我猜这两个看起来毫无意义。 Why don't you just do this in your action:
你为什么不在你的行动中这样做:
[AjaxOnly]
public PartialViewResult Rejestracja()
{
var model = new Logowanie();
return PartialView("Rejestracja", model);
}
and then your ajax call would look like this: 然后你的ajax调用看起来像这样:
$.ajax({
url: '@Url.Content("~/Logowanie/Rejestracja")',
dataType: "html",
type: "GET",
error: function () {
// Handle errors here...
},
success: function (html) {
$("#someDivOnYourPage").html(html);
}
});
this way the entire partial view will be placed in the div of your choice on the page... 这样整个局部视图将被放置在页面上您选择的div中...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.