我正在使用 C# 构建 MVC 4 应用程序,并且我一直在执行大量添加和编辑 View ,这些 View 彼此非常相似。我的问题是如何将相同的 View 与通过填写表单返回的对象的模型重用以添加和编辑项目。这是我在应用程序中所做的示例:
@model namespace.Register
@{
ViewBag.Title = "Manage User";
}
<div class="col1 manage-user">
<div>
@using (Html.BeginForm())
{
<h1>Add User</h1>
<div id="error" class="clear">@Html.ValidationSummary(true)</div>
<ul class="clear">
<li><label for="UserName">Username:</label>
@Html.EditorFor(x => x.UserName)
</li>
<li><label for="Password">Password:</label>
@Html.EditorFor(x => x.Password)
</li>
<li><label for="ConfirmPassword">Confirm password:</label>
@Html.EditorFor(x => x.ConfirmPassword)
</li>
<li>
<label for="level">Level:</label>
@Html.DropDownList("levels", Model.Elements.Levels)
</li>
</ul>
<div class="buttons">
<input type="submit" class="button create" value="Save" name="Save"/>
<input type="submit" class="button back" value="Back" name="Back" formnovalidate/>
</div>
}
</div>
和我的添加方法:
public ActionResult AddUser()
{
Levels levels = new Levels(MyRepository.GetLevelsForUser(User.Identity.Name));
Register model = new Register(levels);
return View(register);
}
[HttpPost]
public ActionResult AddUser(Register model, FormCollection form)
{
if(form["Back"] != null)
{
return RedirectToAction("UserList");
}
if (ModelState.IsValid)
{
... add user logic
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "Please enter valid username and password.");
return View(model);
}
当我编辑时,我需要打开与 AddUser 相同的 View ,但填充模型中的正确属性并发布以保存模型中的更改。目前我使用 ViewData["Username"] , ||-|| “密码”,所以要做到这一点,是否有更好的方法来重用此 AddUser View ?
寄存器模型如下
public class Register
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public Levels Levels { get; set; }
public Register()
{
UserName = null;
Password = null;
ConfirmPassword = null;
Levels = null;
}
public Register(Levels levels)
{
UserName = null;
Password = null;
ConfirmPassword = null;
Levels = levels;
}
}
最佳答案
我通常利用 EditorTemplate
功能来实现此目的,在专用的部分 View 中解耦公共(public)部分。
遵循基于您的代码的示例(请注意每个标签都不同的 BeginForm
标签,而表单“meat”是从部分中挖掘出来的):
注意:这只是一个示例,可能可以删除更多内容,但它更多的是概念证明。
添加 View cshtml
@model namespace.Register
@{
ViewBag.Title = "Manage User";
}
<div class="col1 manage-user">
<div>
@using (Html.BeginForm("AddUser", "YourController", FormMethod.Post))
{
<h1>Add User</h1>
@Html.EditorFor(model => Model)
<div class="buttons">
<input type="submit" class="button create" value="Create user" name="Save"/>
<input type="submit" class="button back" value="Back" name="Back" formnovalidate/>
</div>
}
</div>
编辑 View cshtml
@model namespace.Register
@{
ViewBag.Title = "Manage User";
}
<div class="col1 manage-user">
<div>
@using (Html.BeginForm("UpdateUser", "YourController", FormMethod.Post)
{
<h1>Edit User</h1>
@Html.EditorFor(model => Model)
<div class="buttons">
<input type="submit" class="button create" value="Update user" name="Save"/>
<input type="submit" class="button back" value="Back" name="Back" formnovalidate/>
</div>
}
</div>
上面的两个 View 都会利用这个文件(名称不是虚构的,它是由 MVC 命名约定决定的)。
\Views\Shared\EditorTemplates\Register.cshtml
@model namespace.Register
<div id="error" class="clear">@Html.ValidationSummary(true)</div>
<ul class="clear">
<li><label for="UserName">Username:</label>
@Html.EditorFor(x => Model.UserName)
</li>
<li><label for="Password">Password:</label>
@Html.EditorFor(x => Model.Password)
</li>
<li><label for="ConfirmPassword">Confirm password:</label>
@Html.EditorFor(x => Model.ConfirmPassword)
</li>
<li>
<label for="level">Level:</label>
@Html.DropDownList("levels", Model.Elements.Levels)
</li>
</ul>
这也适用于显示数据,应用完全相同的原理,但部分内容位于 \Views\Shared\DisplayTemplates
中并通过 DisplayFor()
使用。
您可以为模板指定任何您想要的名称,但如果它与类型不匹配,则必须使用 EditorFor
/ 的重载DisplayFor
它将 View 名称作为参数。
关于c# - 如何在管理 MVC 4 应用程序中重用模型和 View 进行添加和编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20327977/