阿贾克斯并没有在MVC局部视图中检索数据 [英] Ajax does not retrieves data in partial view in mvc
问题描述
我在下面我code我打电话与阿贾克斯的局部视图,但是当我的产品点击链接名称的产品的描述不是通过AJAX执行时的Ajax和错误恢复。我检索由用户在同一页面上所选项目的细节,但它没有恢复。请给其中所产生的问题,因为我是新来的MVC任何建议。谢谢...
Create.cshtml
@model名单< PartialView.Models.tbl_product><!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<标题>创建< /标题>
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.5.1.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$('味精')。点击(函数(){
VAR ID = this.id;
$阿贾克斯({
URL:/分类/显示器,
数据:{数据:ID},
成功:函数(MYDATA){
$(#链接),空()追加(MYDATA)。
},
错误:功能(MYDATA){警报(错误); },
键入:POST
});
返回false;
});
});
< / SCRIPT>
< /头>
<身体GT;
@foreach(以型号VAR项)
{
&所述;类别=msg中的href =#ID =@ item.ProductId> @ item.ProductName&下; / A>
}
< DIV ID =链接>
< / DIV>
< /身体GT;
< / HTML>
ClicksUs.cshtml(PartialView)
@model名单< PartialView.Models.tbl_product>@foreach(以型号VAR项)
{
@ items.ProductDesc
}
CategoryController.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用PartialView.Models;命名空间PartialView.Controllers
{
公共类CategoryController:控制器
{
dbEntities dbentity =新dbEntities(); 公众的ActionResult的Create()
{
返回查看(dbentity.tbl_product.ToList());
} 公众的ActionResult显示(int数据)
{
VAR的查询= dbentity.tbl_product.First(C => c.ProductId ==数据);
返回PartialView(ClicksUC查询);
} }}
您详细信息
控制器动作在这里选择一个元素(因为你调用 。首先()
)
公众的ActionResult显示(int数据)
{
VAR的查询= dbentity.tbl_product.First(C => c.ProductId ==数据);
返回PartialView(ClicksUC查询);
}
所以查询变量的类型是 tbl_product
,而不是列表< tbl_product方式>
在另一方面你部分的模式是列表< PartialView.Models.tbl_product>
这显然是错误的。
您部分的模式应该是一个 tbl_product
:
@model PartialView.Models.tbl_product
@ Model.ProductDesc
关于你的局部视图名称错字说
呵呵,什么人。
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. 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. thanks...
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);
}
}
}
Your Details
controller action selects a single element here (because you are calling .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>
.
On the other hand your partial's model is List<PartialView.Models.tbl_product>
which is obviously wrong.
Your partial's model should be a single tbl_product
:
@model PartialView.Models.tbl_product
@Model.ProductDesc
Oh and what others said about the typo in your partial view name.
这篇关于阿贾克斯并没有在MVC局部视图中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!