gpt4 book ai didi

c# - ASP.NET MVC3 Automapper View 模型/模型 View 验证

转载 作者:太空狗 更新时间:2023-10-29 22:05:51 26 4
gpt4 key购买 nike

(同样,一个 MVC 验证问题。我知道,我知道......)

我想使用 AutoMapper ( http://automapper.codeplex.com/ ) 来验证我的创建 View 中不在我的数据库中(因此不在我的 DataModel 中)的字段。

示例:我有一个帐户/创建 View 供用户创建新帐户,我需要密码和确认密码字段,因此用户必须输入密码两次以进行确认。

数据库中的 Account 表如下所示:

Account[Id(PK), Name, Password, Email]

我已经生成了一个 ADO.NET 实体数据模型,然后我使用 ADO.NET 自跟踪实体生成器从中生成了模型。

我还为 [Required] 等验证注释编写了自定义 AccountViewModel。

总而言之,这是我的项目结构:

Controllers:
AccountController

Models:
Database.edmx (auto-generated from database)
Model.Context.tt (auto-generated from edmx)
Model.tt (auto-generated from edmx)
AccountViewModel.cs

Views:
Account
Create.cshtml

我的 AccountViewModel 的代码如下所示:

public class AccountViewModel
{
public int Id { get; set; }

[Required]
public string Name { get; set; }

[Required]
public string Password { get; set; }

[Required]
[Compare("Password")]
public string ConfirmPassword { get; set; }
}

现在,我的创建 View 如下所示:

@model AutoMapperTest.Models.Account
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Account</legend>
<div class="editor-label">
Name
</div>
<div class="editor-field">
@Html.TextBox("Name")
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
Email
</div>
<div class="editor-field">
@Html.TextBox("Email")
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
Password
</div>
<div class="editor-field">
@Html.TextBox("Password")
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
Confirm your password
</div>
<div class="editor-field">
@Html.TextBox("ConfirmPassword");
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

我的代码失败是因为我的模型当然不包含 ConfirmPassword 字段。现在,一只小鸟对我低声说 AutoMapper 可以为我解决这个问题。但我无法弄清楚......有人可以告诉我我必须做什么才能完成这项工作吗?我的 AccountController 现在看起来像这样:

private readonly AccountViewModel _viewModel = new AccountViewModel();
private readonly DatabaseEntities _database = new DatabaseEntities();

//
// GET: /Account/Create

public ActionResult Create()
{
Mapper.CreateMap<AccountViewModel, Account>();
return View("Create", _viewModel);
}

//
// POST: /Account/Create

[HttpPost]
public ActionResult Create(AccountViewModel accountToCreate)
{
try
{
if (ModelState.IsValid)
{
var newAccount = new Account();
Mapper.Map(accountToCreate, newAccount);
_database.Account.AddObject(newAccount);
_database.SaveChanges();
}

return RedirectToAction("Index");
}
catch
{
return View();
}
}

但这行不通...(从 http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx 中获取示例)

谁能请教我这件事吗?非常感谢,对于文字墙和关于同一主题的数百个问题,我深表歉意......

最佳答案

关于您的代码的一些评论:

  1. 您的 View 是针对 @model 的强类型(Account 声明)型号,而应输入 AccountViewModel View 模型(如果不在 View 中使用它,则声明 View 模型毫无意义)。
  2. AutoMapper 不用于验证,仅用于类型之间的转换
  3. 您无需声明 readonly Controller 内 View 模型 ( AccountViewModel ) 的字段。您可以在 GET 操作中实例化 View 模型,并保留默认模型联编程序,将其实例化为 POST 操作的操作参数。
  4. 您应该只为整个应用程序(最好在 Mapper.CreateMap<TSource, TDest> 中)而不是在 Controller 操作中执行一次 AutoMapper 配置(Application_Start)
  5. 您的 View 模型中没有电子邮件字段,这可能是更新失败的原因(特别是如果您的数据库中需要此字段)

因此,您的代码可能如下所示:

public ActionResult Create()
{
var model = new AccountViewModel();
return View("Create", model);
}

[HttpPost]
public ActionResult Create(AccountViewModel accountToCreate)
{
try
{
if (ModelState.IsValid)
{
var newAccount = Mapper.Map<AccountViewModel, Account>(accountToCreate);
_database.Account.AddObject(newAccount);
_database.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}

关于c# - ASP.NET MVC3 Automapper View 模型/模型 View 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com