【问题标题】:Ajax does not retrieves data in partial view in mvcAjax 不在 mvc 的部分视图中检索数据
【发布时间】:2013-11-24 07:00:52
【问题描述】:

我在下面的代码中使用 ajax 调用部分视图,但是当我单击产品名称的链接时,不会通过 ajax 检索到该产品的描述,并且会执行 ajax 错误。我正在检索用户在同一页面上选择的项目的详细信息,但未检索到。请给出任何建议,因为我是 MVC 新手,所以问题出在哪里。谢谢...

创建.cshtml

@model List<PartialView.Models.tbl_product>

<!DOCTYPE html>
<html>
<head>
    <title>Create</title>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.msg').click(function () {
                var id = this.id;
                $.ajax({
                    url: "/Category/Display",
                    data: { data: id },
                    success: function (mydata) {
                        $("#link").empty().append(mydata);
                    },
                    error: function (mydata) { alert("error"); },
                    type: "POST"
                });
                return false;
            });
        });
    </script>
</head>
<body>
@foreach (var item in Model)
{ 
<a class="msg" href="#" id="@item.ProductId">@item.ProductName</a>
}
<div id="link">
</div>
</body>
</html>

ClicksUs.cshtml(部分视图)

@model List<PartialView.Models.tbl_product>

@foreach(var items in Model)
{ 
    @items.ProductDesc
}

CategoryController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartialView.Models;

namespace PartialView.Controllers
{
    public class CategoryController : Controller
    {
        dbEntities dbentity = new dbEntities();

        public ActionResult Create()
        {
            return View(dbentity.tbl_product.ToList());
        }

        public ActionResult Display(int data)
        {
            var query = dbentity.tbl_product.First(c => c.ProductId == data);
            return PartialView("ClicksUC", query);
        }

    }

}

【问题讨论】:

  • 你能发布你从 ajax 调用中得到了什么
  • Display 方法获取 id 假设如果我单击 id 为 2 的链接,则 Display 方法正在获取该 id 并查询检索 id 为 2 的项目的详细信息,但是当 ajax 中的控件函数进入错误块,如果我打印 alert(mydata) 它打印 [object Object] 请给出任何建议..
  • 用json返回部分视图:return View("PartialViewName",jsonData)
  • 在这一行return PartialView("ClicksUC", query); 部分视图的名称是ClicksUC。但是你之前说的名字是ClicksUs.cshtml。我认为是拼写错误。
  • 它的 ClicksUC.cshtml 我认为 ajax 代码有任何错误,因为该值是从控制器方法正确返回的,但部分视图没有更新,当我单击时它不显示任何记录在链接上..

标签: c# jquery ajax asp.net-mvc asp.net-mvc-3


【解决方案1】:

您的Details 控制器操作在这里选择了一个元素(因为您正在调用.First()):

public ActionResult Display(int data)
{
    var query = dbentity.tbl_product.First(c => c.ProductId == data);
    return PartialView("ClicksUC", query);
}

所以查询变量的类型是tbl_product而不是List&lt;tbl_product&gt;

另一方面,您的局部模型是List&lt;PartialView.Models.tbl_product&gt;,这显然是错误的。

您的部分模型应该是单个tbl_product

@model PartialView.Models.tbl_product
@Model.ProductDesc

哦,其他人对您的局部视图名称中的错字怎么说。

【讨论】:

  • +1 darin,我正在编辑以添加该内容,但您说的是第一个(关于视图中的单数表);)
【解决方案2】:

您可以解决代码中的三个问题。

  • 一个是错字(partialview叫做ClicksUS,NOT ClicksUC),
  • 另一个和你返回数据的方式有关
  • 第三个是你使用type: "POST",你应该把它改成type: "GET"

尝试将代码更改为:

public ActionResult Display(int data)
{
    // using First() would have caused you an error in the view if not found
    // still not perfect below, but a step closer 
    var query = dbentity.tbl_product.FirstOrDefault(c => c.ProductId == data);
    // You had ClicksUC, rather than ClicksUS
    return PartialView("ClicksUS", query);
}

我还强烈建议您为数据创建一个ViewModel,而不是从数据库中传递对象,因为这将允许您准确控制应该查看的数据以及应该如何格式化等。

[编辑] 此外,正如 Darin 所说,基于重新运行的单行,您应该将部分视图模型类型更改为:

@model PartialView.Models.tbl_product

【讨论】:

  • 正确 :-) - firstordefault 是我的意思 - 周日早上的疯狂......原谅!
猜你喜欢
  • 2017-06-02
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 2018-11-30
相关资源
最近更新 更多