简体   繁体   English

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

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

I my below code i am calling partial view with ajax but when i click on the link of product Name the description of that product is not retrieved through ajax and error of ajax executes. 我的下面的代码我用ajax调用部分视图但是当我点击产品名称的链接时,不会通过ajax检索该产品的描述并执行ajax的错误。 I am retrieving the details of items selected by user on the same page but it is not retrieved. 我正在检索用户在同一页面上选择的项目的详细信息,但不会检索它。 Please give any suggestion where is the issue arising because i am new to MVC. 请提出任何建议,因为我是MVC的新手。 thanks... 谢谢...

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

@model List<PartialView.Models.tbl_product>

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

CategoryController.cs 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);
        }

    }

}

Your Details controller action selects a single element here (because you are calling .First() ): 您的Details控制器操作在此处选择单个元素(因为您正在调用.First() ):

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

So the type of the query variable is tbl_product and not List<tbl_product> . 因此查询变量的类型是tbl_product而不是List<tbl_product>

On the other hand your partial's model is List<PartialView.Models.tbl_product> which is obviously wrong. 另一方面,你的部分模型是List<PartialView.Models.tbl_product> ,这显然是错误的。

Your partial's model should be a single tbl_product : 您的部分模型应该是单个tbl_product

@model PartialView.Models.tbl_product
@Model.ProductDesc

Oh and what others said about the typo in your partial view name. 哦,其他人对你的部分名字中的拼写错误说了些什么。

there are three issues in the code that you could address. 您可以解决的代码中有三个问题。

  • One is a typo (the partialview is called ClicksUS, NOT ClicksUC), 一个是拼写错误(部分视图称为ClicksUS,而不是ClicksUC),
  • the other is related to the way you return the data 另一个与返回数据的方式有关
  • the third is that you use the type: "POST" , you should change this to type: "GET" . 第三个是你使用type: "POST" ,你应该改为type: "GET"

try changing the code to: 尝试将代码更改为:

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

I'd also strongly suggest that you create a ViewModel for your data, rather than passing the objects from the database as this will allow you to control exactly the data that should be viewed and how it should be formatted etc. 我还强烈建议您为数据创建一个ViewModel ,而不是从数据库传递对象,因为这样可以准确控制应该查看的数据以及应该如何格式化等。

[edit] Also, as Darin says, based on a single row being retruned, you should change your partial view model type to: [编辑]另外,正如Darin所说,基于要重新编译的单行,您应该将部分视图模型类型更改为:

@model PartialView.Models.tbl_product

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

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