执行Html.BeginForm时维护模型

时间:2018-09-27 11:14:42

标签: c# asp.net-mvc razor

我有一个razor语法html页面,其中显示了一些从控制器传递到视图的模型数据。呈现数据后,用户可以单击与我的模型中某个项目相关的按钮,因此将基于单击的按钮显示更多与项目相关的数据。

要实现此目的,我尝试将ID(整数)发布到控制器,因此控制器将写入ViewData并使用 same 模型重新加载页面,但是现在该页面将显示来自模型的更多信息(因为用户单击了按钮。)。但是我无法弄清楚如何维护相同的复杂模型对象。

我的控制器:

    // The Entry point to the controller
    [Route("[controller]/[action]/cardNumber")]
    [HttpPost]
    public IActionResult Inspect(string cardNumber)
    {
        var dataObject = GetDataObjectModel();

        return View(dataObject);
    }

    [Route("[controller]/[action]/model/ruleId")]
    [HttpPost]
    public IActionResult ExpandRules(int ruleId)
    {
        ViewData["RuleId"] = ruleId;
        return View("Inspect"); // Somehow need to use same model from above here
    }

Inspect.cshtml:

<table>
            <caption>Rules</caption>
            <thead>
            <tr>
                <th>Expand DoorGroups</th>
                <th>Rule Name</th>
                <th>Exists in Database</th>
                <th>Exists in Staging Card</th>
                <th>Exists in Proxy</th>
                <th>Exists in Controller</th>
            </tr>
            </thead>
            <tbody>

            @if (Model.RulesFromDb != null)
            {
                foreach (var rule in Model.RulesFromDb)
                {
                    <tr>
                        <td>
              <!-- THE BUTTON -->
                            @using (Html.BeginForm("ExpandRules", "Inspector", new {ruleId = rule.Id}, FormMethod.Post))
                            {
                                <input type="submit" value="+"/>
                            } 
                        </td>
                        <td>
                            @Html.DisplayTextFor(model => rule.Name)
                        </td>
                        <td>
                            V
                        </td>
                        <td>
                            @(Model.RulesFromStaging.Exists(x => x.Id == rule.Id) ? "V" : "X")
                        </td>
                        <td>
                            @if (Model.RulesFromProxy != null)
                            {
                                @(Model.RulesFromProxy.Exists(x => x.Id == rule.Id) ? "V" : "X")
                            }
                            else
                            {
                                @Html.Raw("Data not Provided")
                            }
                        </td>
                        <td>
                            @if (Model.RulesFromController != null)
                            {
                                @(Model.RulesFromController.Exists(x => x.Id == rule.Id) ? "V" : "X")
                            }
                            else
                            {
                                @Html.Raw("Data not Provided")
                            }
                        </td>
                    </tr>
                }
            }
            </tbody>
        </table>

0 个答案:

没有答案