简体   繁体   English

MVC3在与项目列表相同的页面上显示编辑视图

[英]MVC3 Display the edit view on the same page as the list of items

I have a list of transactions. 我有交易清单。 When I click one of them, I'd like to be able to display the edit form (populated with the selected item's data) to the right of the list. 当我单击其中之一时,我希望能够在列表的右侧显示编辑表单(填充有所选项目的数据)。 Any ideas? 有任何想法吗?

Below is the partial view that displays the list of transactions. 下面是显示事务列表的局部视图。

@model IEnumerable<BudgetPlus.Models.Transaction>
@{
    ViewBag.Title = "Index";
}
<p>
    @Html.ActionLink("Create New", "Create", null, new { @class = "button-link" })
</p>
<table>
    <thead>
        <tr>
            <th class="column-Date">Date</th>
            <th class="column-Description">Description</th>
            <th class="column-Category">Category</th>
            <th class="column-Amount">Amount</th>
            <th class="action-button"></th>
            <th class="action-button"></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Category.DisplayName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Amount)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id })
                </td>
                <td>
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
        }
    </tbody>
</table>
<div>
    @Html.EditorForModel("Edit")
</div>

And this is what the edit method in my TransactionController looks like. 这就是我TransactionController中的edit方法的样子。

    public ActionResult Edit(int id)
    {
        Transaction transaction = db.Transactions.Find(id);
        ViewBag.CategoryId = new SelectList(db.Categories, "Id", "DisplayName", transaction.CategoryId);
        return View(transaction);
    }

Probably the easiest way to achieve this is to use Ajax.Actionlink helper. 实现此目的最简单的方法可能是使用Ajax.Actionlink帮助器。

In your for loop have this instead of Html.Actionlink: 在您的for循环中,用它代替Html.Actionlink:

@Ajax.ActionLink("Edit", "Edit", new { id=item.Id }, new AjaxOptions { UpdateTargetId = "editor", HttpMethod = "Get", InsertionMode = InsertionMode.Replace });

And on the right side of your table: 在表格的右侧:

<div id="editor"></div>

Then have a partial view for editing the Transaction, which will be rendered into the div with id "editor". 然后有一个用于编辑事务的局部视图,该视图将呈现为ID为“ editor”的div。 You are using EditorForModel helper so I assume you have a partial view for editing already. 您正在使用EditorForModel助手,因此我假设您已经有部分视图可用于编辑。

In your actionmethod return the partial view instead: 在您的操作方法中,返回部分视图:

return PartialView("YourPartialView", transaction);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM