使用带有可变数据的 Jquery Ajax 渲染局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6352302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:43:50  来源:igfitidea点击:

Render Partial View using Jquery Ajax with variable data

asp.net-mvc-3jqueryrenderpartial

提问by dan_vitch

I have already read this post, but I am not sure know to make it work taking data from the user. Here is the ajax jquery I am using. I know (or at least think) that this cant render a partial. But it works all the way until the render fails. I thought it may be helpful to have.

我已经阅读了这篇文章,但我不确定如何从用户那里获取数据。这是我正在使用的 ajax jquery。我知道(或至少认为)这不能呈现部分。但它一直有效,直到渲染失败。我认为拥有它可能会有所帮助。

 $.ajax(
     {
         type: 'POST',
         contentType: 'application/json; charset=utf-8',
         data: "{'test':" + "'" + dateText + "'}",
         dataType: 'json',
         url: 'Site/Grab/',
         success: function (result) {
         alert('Success');
         },

         error: function (error) {
            alert('Fail');
         }
      });

Here is my controller

这是我的控制器

[HttpPost]
    public ActionResult Grab(string test)
    {
        DateTime lineDate= Convert.ToDateTime(test);
        List<Info> myInfo= GameCache.Data(lineDate);
       return PartialView("_PartialView", myInfo);
    }

回答by RPM1984

Okay, couple of things to try:

好的,有几件事要尝试:

1) dataTypeis the expected result of the ajax call. In your case, your sendingJSON, but receivingHTML. The content-typeparameter specifies the request, which you have (and what you have is correct). So the data type should be:

1)dataType是ajax调用的预期结果。在您的情况下,您发送JSON,但接收HTML。该content-type参数指定了您拥有的请求(以及您拥有的请求是正确的)。所以数据类型应该是:

dataType: 'html',

2) You need to serialize the JSON. Try grabbing the lightweight JSONlibrary and stringify'ing:

2)您需要序列化JSON。尝试获取轻量级JSON库并进行字符串化

var test = { test: 'testvalue' };
$.ajax {
   ...
   data: JSON.stringify(test),
   ...
});

Much easier than trying to coerce a JSON string with quoatations. Create a regular JS variable, then stringify it.

比尝试使用引号强制转换 JSON 字符串要容易得多。创建一个常规 JS 变量,然后对其进行字符串化。

The rest of your code looks fine.

您的其余代码看起来不错。

If it's a problem with the HTML/markup of the partial view itself, run in debug mode and Visual Studio should stop on the line in the markup that is causing the problem.

如果部分视图本身的 HTML/标记有问题,请在调试模式下运行,Visual Studio 应该在导致问题的标记行上停止。

Bonus Hint: ASP.NET MVC 3 includes built-in JSON model binding. So you can create a basic POCO that matches the fields of your JSON object, then accept it as a strongly-typed object in the action method:

额外提示:ASP.NET MVC 3 包括内置的 JSON 模型绑定。因此,您可以创建一个与 JSON 对象的字段匹配的基本 POCO,然后在 action 方法中将其作为强类型对象接受:

[HttpPost]
public ActionResult Grab(MyJsonObject obj) 
{
   DateTime lineDate= Convert.ToDateTime(obj.test);
   List<Info> myInfo= GameCache.Data(lineDate);
   return PartialView("_PartialView", myInfo);
}

Since your only sending one parameter, it's overkill - but if you have more than 2 then it's worthwhile using a JSON POCO.

由于您只发送一个参数,因此有点矫枉过正 - 但如果您有 2 个以上,那么使用 JSON POCO 是值得的。

回答by Rashid Hussain Siddiqui

Change your controller code to:

将您的控制器代码更改为:

public ActionResult Grab(string test) {
  DateTime lineDate= Convert.ToDateTime(test);
  List<Info> myInfo= GameCache.Data(lineDate);

  return Json(new { data = this.RenderPartialViewToString("_PartialView", myInfo) });
}