【发布时间】: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