sách gpt4 ai đã đi

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

In lại Tác giả: Space Dog 更新时间:2023-10-29 22:05:51 28 4
mua khóa gpt4 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



@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

Account

Name


@Html.TextBox("Name")
@Html.ValidationMessageFor(model => model.Name)


Email


@Html.TextBox("Email")
@Html.ValidationMessageFor(model => model.Email)


Password


@Html.TextBox("Password")
@Html.ValidationMessageFor(model => model.Password)


Confirm your password


@Html.TextBox("ConfirmPassword");
@Html.ValidationMessageFor(model => model.ConfirmPassword)





}

@Html.ActionLink("Back to List", "Index")

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

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

//
// GET: /Account/Create

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

//
// POST: /Account/Create

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

return RedirectToAction("Index");
}
nắm lấy
{
return View();
}
}

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

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

1 Câu trả lời

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

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

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

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

[HttpPost]
public ActionResult Create(AccountViewModel accountToCreate)
{
thử
{
if (ModelState.IsValid)
{
var newAccount = Mapper.Map(accountToCreate);
_database.Account.AddObject(newAccount);
_database.SaveChanges();
}
return RedirectToAction("Index");
}
nắm lấy
{
return View();
}
}

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

28 4 0
Bài viết được đề xuất: c# - 如何将通用 List 转换为 List?
Bài viết được đề xuất: php - 用python加密数据,用php解密
Bài viết được đề xuất: c# - ObservableCollection 优于 ObjectSet
Bài viết được đề xuất: python - 根据另一个列值更改 pandas DataFrame 列值
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com