MVC执行顺序
只是想同时学习 MVC2/.net 4.0。当您开始“MVC 2 Web”项目时,我只是使用 VS 附带的通用模板(即为您设置了帐户控制器和家庭控制器)。
所以我的问题是视图又是一个强类型的模型。该模型看起来像这样:
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterModel {
[Required]
[DisplayName("User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[DisplayName("Email address")]
public string Email { get; set; }
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Confirm password")]
public string ConfirmPassword { get; set; }
[Required]
[DisplayName("School")]
public string School { get; set; }
}
然后我想我在网页上按“注册”,它会从我的控制器执行以下操作:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View(model);
}
所以有几个问题。
1)方法名称上方[]的类..它们是否首先执行(我不知道这里使用什么术语)?例如,模型的属性上方有一个 [ValidatePasswordLength]
。这是否意味着在提交未验证的密码后,它甚至不会击中控制器?我也可以将这个逻辑放入控制器中吗?
2) ModelState 类来自哪里?
我只是想以流程图的方式了解 MVC 中的所有内容是如何连接的。好像绕了一个大圈,没有起点。
Just trying to learn MVC2/ .net 4.0 at the same time. I am just using the generic template VS comes with when you start with a "MVC 2 Web" project (ie, the account controller and home controllers are setup for you).
So my question is that the view is strongly typed again a model. The model looks like this:
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterModel {
[Required]
[DisplayName("User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[DisplayName("Email address")]
public string Email { get; set; }
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Confirm password")]
public string ConfirmPassword { get; set; }
[Required]
[DisplayName("School")]
public string School { get; set; }
}
then I guess I press "register" on my web page and it executes the following from my controller:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View(model);
}
so a couple of questions.
1) The classes that are [] above the method name.. are they executed (i dont know what term to use here) first? For example the model has a [ValidatePasswordLength]
above its property. Does that mean upon submitting a password that dosnt validate, it dosnt even hit the controller? Can I also put this logic in the controller?
2) Where is ModelState
class coming from ?
I just kinda wanna know in a flow chart way on how everything is connected in MVC. It seems like its a big circle, and there is no starting point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性并不像检查那样被执行。
在模型绑定期间,将扫描视图模型的属性,模型绑定器获取这些属性的列表,然后可以更改其行为(例如,[bind] 属性会影响模型绑定器是否尝试填充给定属性)或调用类(例如验证属性)。
具体回答您的问题:
1)验证可以在两个地方进行,要么在调用操作之前,即当您的操作采用视图模型时,要么在调用 TryValidateModel 时显式地在操作中进行。无论以哪种方式调用操作,都取决于您检查有效性并在操作中相应地处理响应,就像您在上面的操作中所做的那样。
2)ModelState来自ModelBinder。
了解 MVC 工作原理的最简单方法是下载源代码、调试并单步执行请求。
Attributes aren't executed so much as checked.
During modelbinding the view model will be scanned for attributes, the model binder gets a list of these attributes and can then change its behaviour (eg the [bind] attribute affects if the model binder will try and populate a given property) or call the class (eg Validation attributes).
To answer your questions specifically:
1)Validation can happen in two places, either before the action is called, ie when your action takes a view model, or explicitly in the action when you call TryValidateModel. Either way the action is called, it is up to you to check validity and handle the response accordingly within the action as you did in your action above.
2) ModelState comes from the ModelBinder.
The easiest way to see how MVC works is to download the source, debug and step through a request.