【问题标题】:How to get two different models on one page with Partial Views?如何使用局部视图在一个页面上获取两个不同的模型?
【发布时间】:2023-03-18 07:10:01
【问题描述】:

我有一个 UserManager 页面,管理员可以在其中获取当前已注册的所有帐户、删除帐户、更新帐户和注册新帐户。

我有一个控制器,它将 db.users 中的所有用户重新发送到一个列表中。

public ActionResult UserManager()
{
    if (User.IsInRole("1"))
    {     
        var db = new ReviewLogsTestDBEntities();
        return View(db.Users.ToList());                
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }    
}

这很好,我的删除工作正常。
当我想在同一页面上获取注册表时,问题就来了。

这就是我现在的看法:

@model IEnumerable<ReviewLogs.User>

@{    
    ViewBag.Title = "UserManager";
}

<h2>UserManager</h2>    

<div id="userDetails">
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UserName);
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Role)
            </th>

            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.UserName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Role)
                </td>

                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |                      
                    @Html.ActionLink("Delete", "Del", new { id = item.ID}, new { onclick = "return confirm('Are you sure you wish to delete this article?')"});
                </td>
            </tr>
        }

    </table>
    </div>

如您所见,有一个模型:

@model IEnumerable<ReviewLogs.User>

但我希望我的表单使用它自己的 RegisterModel,其中包含需要填写的必填字段、正则表达式等。当用户按下提交时,UserManager 页面被重新加载,现在显示新添加的用户。

我想我可以创建一个局部视图:

@model ReviewLogs.Models.RegisterModel

@{
    ViewBag.Title = "Register";
    ViewBag.SubHead = "Register";
}

@Html.ValidationSummary(true, "Creation of new Account has failed please check your fields.")
<p id="success">@ViewBag.SuccessMessage</p>
<br />
@using (Html.BeginForm())
{
    //The Registration Page the user sees


    //The userName label and textbox
    <div class="inputFields">
        @Html.LabelFor(model => model.userName)
        @Html.TextBoxFor(model => model.userName)
        @Html.ValidationMessageFor(model => model.userName)
    </div>

    //The accountType label and radioButton
    <div class="radioButtonAccountType">
        @Html.LabelFor(model => model.accountType)
        @Html.RadioButtonFor(model => model.accountType, "1")<span class="adminLabel">Admin</span>
        @Html.RadioButtonFor(model => model.accountType, "2")<span class="userLabel">User</span>
        @Html.ValidationMessageFor(model => model.accountType)
    </div>

    <input class="submitButton" type="submit" value="Create User" style="margin-left:140px;margin-bottom: 20px;" />

}

在我的 UserManager 视图中我添加了:

@Html.Partial("Register");

但后来我得到了这个错误:

传入字典的模型项的类型为“System.Collections.Generic.List`1[ReviewLogs.User]”,但此字典需要“ReviewLogs.Models.RegisterModel”类型的模型项。

我怎样才能通过这个以便我能够发布我的注册?

【问题讨论】:

  • 您的局部视图的模型是 RegisterModel,所以这是正确的行为。您需要将表单放在其他页面上

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


【解决方案1】:

查找接受模型的 @Html.Partial 方法重载,并通过以下方式调用它:

@Html.Partial("Register", Model.RegisterModel)

为了使其工作,最好的办法可能是创建一个新模型,然后将其传递到 UserManager 视图中。此 UserManagerModel 将具有两个属性:IEnumerable&lt;ReviewLogs.User&gt;,供 UserManagerView 本身使用,以及您将传递到 Partial 视图的RegisterModel

【讨论】:

    【解决方案2】:

    只需在您的视图中新建一个寄存器模型的副本并将其传递给部分:

    @Html.Partial("Register", new Namespace.To.RegisterModel())
    

    只需确保将用于注册帖子的表单设置为单独的操作即可。您应该尝试让这一操作处理多种不同类型的帖子。您可以在表单的隐藏字段中传递返回 URL(基本上只是 Request.RawUrl),然后如果存在此值,您可以使用它重定向回同一页面。例如:

    @Html.Hidden("returnUrl", Request.RawUrl)
    

    那么,

    [HttpPost]
    public ActionResult Register(RegisterModel model, string returnUrl)
    {
        // do the register thing
    
        if (!String.IsNullOrWhiteSpace(returnUrl))
        {
            return Redirect(returnUrl);
        }
    
        return RedirectToAction("Index"); // or whatever should be default
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-23
      • 2019-04-21
      • 2016-02-12
      • 2014-07-28
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多