繁体   English   中英

Ajax不会在mvc中以部分视图检索数据

[英]Ajax does not retrieves data in partial view in mvc

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

Create.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(PartialView)

@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);
        }

    }

}

您的Details控制器操作在此处选择单个元素(因为您正在调用.First() ):

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

因此查询变量的类型是tbl_product而不是List<tbl_product>

另一方面,你的部分模型是List<PartialView.Models.tbl_product> ,这显然是错误的。

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

@model PartialView.Models.tbl_product
@Model.ProductDesc

哦,其他人对你的部分名字中的拼写错误说了些什么。

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

  • 一个是拼写错误(部分视图称为ClicksUS,而不是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM