Ajax.ActionLink,如何在局部视图中发送选定的对象? asp.net mvc

时间:2022-04-12 14:27:43

I guess this is a noob question, but here it comes:

我想这是一个菜鸟问题,但它来了:

I have a list of products:

我有一个产品清单:

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.Encode(item.code) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.date)) %>
        </td>
        <td>
            <%= Html.Encode(item.category) %>
        </td>
        <td>
            <%= Html.Encode(item.description) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:F}", item.price)) %>
        </td>
     ......
}

And a partial view after all of these (in the same page):

所有这些之后的部分视图(在同一页面中):

 <div id="productForEdit">
             <fieldset>
                 <legend>Your Selected Product</legend>
                 <% Html.RenderPartial("~/Views/Products/Edit", productObject); %>
             </fieldset>
    </div>

How do I use Ajax.ActionLink, so that when I will click the description of a product, the product will be plugged in the Partial View from the bottom of the page? I tried some combination with UpdateTargetId="productForEdit", but I had no success.

我如何使用Ajax.ActionLink,这样当我点击产品描述时,产品将插入页面底部的部分视图?我尝试了一些与UpdateTargetId =“productForEdit”的组合,但我没有成功。

The purpose is to have a quick edit tool in the page.

目的是在页面中有一个快速编辑工具。

1 个解决方案

#1


1  

I think this should work:

我认为这应该有效:

<td>
  <%= Ajax.ActionLink(Html.Encode(item.description), /* link text */
                      "GetProduct", /* action name */
                      "Product", /* controller name */
                      new { productCode = Model.code }, /* route values */
                      new AjaxOptions() { InsertionMode = InsertionMode.Replace,
                                          UpdateTargetId = "productForEdit" }) %>
</td>

This does expect a ProductController with an action named GetProduct, which takes a parameter productCode. Have this action return the view "Products/Edit". You could also pass the whole product as a parameter to the action method, but that changes nothing to the basic idea. Good luck!

这确实期望ProductController具有名为GetProduct的动作,该动作采用参数productCode。请执行此操作返回“产品/编辑”视图。您还可以将整个产品作为参数传递给操作方法,但这不会改变基本概念。祝你好运!

#1


1  

I think this should work:

我认为这应该有效:

<td>
  <%= Ajax.ActionLink(Html.Encode(item.description), /* link text */
                      "GetProduct", /* action name */
                      "Product", /* controller name */
                      new { productCode = Model.code }, /* route values */
                      new AjaxOptions() { InsertionMode = InsertionMode.Replace,
                                          UpdateTargetId = "productForEdit" }) %>
</td>

This does expect a ProductController with an action named GetProduct, which takes a parameter productCode. Have this action return the view "Products/Edit". You could also pass the whole product as a parameter to the action method, but that changes nothing to the basic idea. Good luck!

这确实期望ProductController具有名为GetProduct的动作,该动作采用参数productCode。请执行此操作返回“产品/编辑”视图。您还可以将整个产品作为参数传递给操作方法,但这不会改变基本概念。祝你好运!