简体   繁体   English

MVC 4如何在单击列表项时将对象传递给局部视图?

[英]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: 你有两个选择:

  1. User @Ajax.ActionLink ( How can i load Partial view inside the view ) User @ Ajax.ActionLink( 如何在视图中加载局部视图

  2. 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: 确保以下几点得到照顾:

  1. You have included your jquery-1.6.2.js or whatever version of this file you have. 您已经包含了jquery-1.6.2.js或此文件的任何版本。

  2. Also make you have included your jquery file you have created. 还要包含您创建的jquery文件。 Make sure the above one is included first. 确保首先包含上述内容。

  3. 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呈现此部分

    1. what you can also do is Make sure that jQuery is not included twice and that you don't have some javascript errors. 您还可以做的是确保jQuery不包含两次,并且您没有一些javascript错误。 Look at your javascript console in the browser as well as the Network tab to ensure that all your scripts are included properly. 查看浏览器中的javascript控制台以及“网络”选项卡,确保正确包含所有脚本。

Good Luck!!! 祝好运!!!

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

相关问题 如何将项目数据从.net MVC WebGrid传递到部分视图 - How to pass item data from .net MVC WebGrid to Partial View 当两个部分视图都包含在 ASP.NET MVC 中的同一父视图中时,如何将数据列表从一个部分视图传递到另一个部分视图? - How to pass a list of data from one partial view to another partial view, when both partial views are contained in a same parent view in ASP.NET MVC? 如何将IEnumerable模型传递给MVC中的局部视图 - How to pass an IEnumerable model to a partial view in MVC 在MVC 5中,如何将表对象数据作为列表传递给Controller View Model? - In MVC 5 how to pass table Object data as a list to Controller View Model? 将对象数据从控制器传递到MVC C#中的部分视图 - Pass object data from controller to Partial View in MVC C# 如何在MVC中循环中将模型数据从视图传递到局部视图 - How to pass model data from view to partial view in loop in MVC MVC 4 - 如何将模型数据传递给局部视图? - MVC 4 - how do I pass model data to a partial view? 如何将倍数和选择性模型传递到MVC剃刀局部视图中? - How to pass multiples and selective Model into MVC razor Partial View? 如何在MVC中呈现HighCharts的局部视图并传递参数? - How to render partial view for HighCharts and pass parameter in MVC? MVC部分视图以显示列表 - MVC Partial view to Display list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM