[英]MVC 4 How to pass an object to partial view when list item is clicked?
这是一个控制器
public class ProductController : Controller
{
//
// GET: /Product/
public ActionResult Index(int id = 0)
{
return View(DALCategory.GetCategoryList());
}
public PartialViewResult productPartial(int id)
{
if (id > 0)
{
return PartialView("_productPartial", DALProduct.GetProductListByCateDetail(id));
}
else
{
return PartialView("_productPartial", DALProduct.GetProductList());
}
}
这是索引视图中的代码
@model IEnumerable<Model.Category>
<h2>Products</h2>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(@item.name, "productPartial", new { id = item.id })
</td>
</tr>
}
</table>
@Html.Partial(productPartial)
这是部分视图的代码。
@model IEnumerable<Model.Product>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.SKU)
</th>
<th>
@Html.DisplayNameFor(model => model.product_name)
</th>
<th>
@Html.DisplayNameFor(model => model.price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.SKU)
</td>
<td>
@Html.DisplayFor(modelItem => item.product_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.price)
</td>
<td>
@* @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })*@
</td>
</tr>
}
</table>
所以我想要做的是......当单击一个项目时,它会使用一个新对象呈现部分。 是的,部分视图仅在同一页面上呈现。 我做了很多事。 虽然我认为这不是一个很难的问题,但我不确定我需要做些什么......
由于您要更新页面的一部分,因此需要使用ajax来完成此操作。 你有两个选择:
User @ Ajax.ActionLink( 如何在视图中加载局部视图 )
您可以编写自己的jquery ajax请求来调用控制器方法并返回局部视图。
它也发生在我身上。 我希望页面是部分的并加载在同一页面上,但它一直转发我到其他链接。 确保以下几点得到照顾:
您已经包含了jquery-1.6.2.js
或此文件的任何版本。
还要包含您创建的jquery文件。 确保首先包含上述内容。
如果您已将这些文件包含在.cshtml
部分的某些部分中,请确保在_Layout.cshtml
或您正在使用的其他布局文件中呈现它。 例如,您已将文件包含在index.cshtml
head部分中,如下所示
@section head {}然后你必须使用@RenderSection("head", required: false)
在_layout.cshtml
呈现此部分
祝好运!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.