【发布时间】:2014-01-21 15:59:14
【问题描述】:
我的礼物控制器有一个动作结果,它在参数中使用一个 GiftViewModel 来检查模型状态。
我刚刚向 GiftViewModel 添加了一个 LoginModel 属性。我想测试这个属性的modelState。
GiftViewModel.cs:
public class GiftViewModel
{
public LoginModel login { get; set; }
[...]
}
GiftController.cs
//
// POST: /Gift/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(GiftViewModel model, string returnUrl)
{
// here instead of the overall modelstate
// I would like to check only the modestate of the login property
// of my model
if (ModelState.IsValid && WebSecurity.Login(model.login.Email, model.login.Password, persistCookie: model.login.RememberMe))
{
return View("Index", model);
}
return View("Index", model);
}
我该如何管理它?
【问题讨论】:
-
你为什么需要那个?您会允许主视图模型的其他属性使用无效值吗?
-
是的,没错,我只想先验证这个“步骤”。
-
没有办法做到这一点。立即验证整个模型。
标签: c# asp.net-mvc-4