[英]MVC 4 How to pass an object to partial view when list item is clicked?
This is a controller 这是一个控制器
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());
}
}
This is code in index view 这是索引视图中的代码
@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)
And this is code for partial view. 这是部分视图的代码。
@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>
So what I want to do is... when an item is clicked, it renders the partial with an new object. 所以我想要做的是......当单击一个项目时,它会使用一个新对象呈现部分。 Yes, the partial view is only rendered on the same page. 是的,部分视图仅在同一页面上呈现。 I have been doing so many things. 我做了很多事。 Although I do not think it is such a hard problem, I am not really sure what I need to do for this... 虽然我认为这不是一个很难的问题,但我不确定我需要做些什么......
Since you want to update a part of the page you will need to use ajax to accomplish that. 由于您要更新页面的一部分,因此需要使用ajax来完成此操作。 You have two options there: 你有两个选择:
User @Ajax.ActionLink ( How can i load Partial view inside the view ) User @ Ajax.ActionLink( 如何在视图中加载局部视图 )
You can write your own jquery ajax request that invokes your controller method and returns the partial view. 您可以编写自己的jquery ajax请求来调用控制器方法并返回局部视图。
It happened to me as well. 它也发生在我身上。 I wanted the page to be partial and loaded on the same page but it kept forwarding me to other link. 我希望页面是部分的并加载在同一页面上,但它一直转发我到其他链接。 Make sure following points are taken care of: 确保以下几点得到照顾:
You have included your jquery-1.6.2.js
or whatever version of this file you have. 您已经包含了jquery-1.6.2.js
或此文件的任何版本。
Also make you have included your jquery file you have created. 还要包含您创建的jquery文件。 Make sure the above one is included first. 确保首先包含上述内容。
If you have included these files in some section of your .cshtml
section then make sure you render it in your _Layout.cshtml
or other layout file you are using. 如果您已将这些文件包含在.cshtml
部分的某些部分中,请确保在_Layout.cshtml
或您正在使用的其他布局文件中呈现它。 For example you have included your file in head section of your index.cshtml
like this 例如,您已将文件包含在index.cshtml
head部分中,如下所示
@section head{ } then you have to render this section in _layout.cshtml
with @RenderSection("head", required: false)
@section head {}然后你必须使用@RenderSection("head", required: false)
在_layout.cshtml
呈现此部分
Good Luck!!! 祝好运!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.