[英]How to get two different models on one page with Partial Views?
我有一个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”。
我怎样才能通过此认证,以便能够过帐我的注册?
查找接受模型的@Html.Partial
方法重载,并通过类似以下方式调用它:
@Html.Partial("Register", Model.RegisterModel)
为了使它起作用,最好的选择是创建一个新模型,然后传递给UserManager视图。 此UserManagerModel将具有两个属性: IEnumerable<ReviewLogs.User>
(由UserManagerView本身使用)和RegisterModel
(将传递给Partial视图)。
只需在您的视图中更新注册模型的副本,然后将其传递给局部模型:
@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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.