【问题标题】:Pass ViewModel to ActionResult mvc4将 ViewModel 传递给 ActionResult mvc4
【发布时间】:2014-03-05 21:11:01
【问题描述】:

我认为模型是 IEnumerable。该视图是在创建具有读写操作的新控制器时创建的 mvc 基本视图。 我不希望编辑操作会调用不同的视图,我想在索引视图中添加一个按钮,通过按下特定行中的按钮,该按钮将使用“按下”的模型调用操作结果,并且从那里的逻辑将继续。

观点

@model IEnumerable<V1_ILotto.Areas.Admin.Models.WithdrawalModel>

@{
    ViewBag.Title = "בקשות משיכה";
}

<h2>בקשות משיכה</h2>

@using (Html.BeginForm("Approve", "Withdrawals", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.User)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.WithdrawalAmount)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Balance)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsApproved)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.User)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.WithdrawalAmount)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Balance)
                </td>

                <td>
                    <p>
                        <input type="submit" value="Submit" />
                    </p>
                </td>

            </tr>
        }
    </table>
}

控制器

[HttpPost]
        public ActionResult Approve(WithdrawalModel withdrawalmodel)
        {
            if (ModelState.IsValid)
            {
                //logic for updating the db
                return RedirectToAction("Index");
            }
            return View(withdrawalmodel);
        }

注意:视图不是获取单个模型,而是该模型的 IEnumerable

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    仅用于助手(除了显示)将数据绑定到模型。如果你想传回数据,你需要将你的值至少隐藏在一个隐藏的 for

    @Html.HiddenFor(x => x.Balance) 
    

    【讨论】:

    • 最后我让它变得更简单了。使用 ActionLink 编辑模式,添加 id 并调用 db 逻辑。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2012-05-13
    • 2014-03-23
    相关资源
    最近更新 更多