[英]How can I manually validate a model in the controller after altering the request using ASP.NET MVC 5?
In have an application that is written on the top on the ASP.NET MVC 5 framework. 在ASP.NET MVC 5框架的顶部编写了一个应用程序。
I have a page with two separate forms (one visible and the other hidden when the page is loaded). 我的页面有两种不同的形式(加载页面时一种可见,另一种隐藏)。 The first form has a drop down menu, if the user selects the option "ABC", I use javascript do display the second form.
第一种形式有一个下拉菜单,如果用户选择选项“ ABC”,我将使用javascript来显示第二种形式。 But if he/she selects any other option, I hide the second form.
但是,如果他/她选择了其他任何选项,我将隐藏第二种形式。
Using javascript, I change the required property for the required field to optional since the form is not visible. 使用javascript,由于表单不可见,因此我将必填字段的必填属性更改为可选。 (this is done only to the client side only)
(仅对客户端执行此操作)
Now, since the second form has some required fields at the server side, every time I submit the form, the property ModelState.IsValid
is always false because the second form has some required field which is not provided. 现在,由于第二个表单在服务器端具有一些必填字段,因此每次我提交表单时,属性
ModelState.IsValid
始终为false,因为第二个表单具有一些未提供的必填字段。
Somehow, before I validate the form, I need to alter the model by setting the SecondForm
property to null then validate. 不知何故,在验证表单之前,我需要通过将
SecondForm
属性设置为null然后进行验证来更改模型。
Here is how my ViewModelCapsule
class look like 这是我的
ViewModelCapsule
类的样子
public class ViewModelCapsule
{
public FirstFormViewModel FirstForm { get; set; }
public SecondFormViewModel SecondForm { get; set; }
}
I tried to work around this issue by using TryValidateModel()
method on my controller like so 我试图通过在控制器上使用
TryValidateModel()
方法来解决此问题,如下所示
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(int id, ViewModelCapsule model)
{
try
{
if(model.FirstModel != null && model.FirstModel.Menu1 != "ABC")
{
model.SecondModel = null
}
if (TryValidateModel(model))
{
// Here I am expecting the form to validate since the first form has valid date
// Do somethig with the request
}
return new RedirectAction("Index");
}
catch (Exception exception)
{
return Content(exception.Message);
}
}
but for some reason TryValidateModel(model)
is also returning false. 但由于某些原因,
TryValidateModel(model)
也返回false。 I evaluated the incoming data and FirstForm
has correct data in all of its properties. 我评估了传入的数据,并且
FirstForm
在其所有属性中都有正确的数据。
How can I manually validate a model in the controller after altering the request? 更改请求后,如何在控制器中手动验证模型?
I believe I got it to work now. 我相信我现在就可以使用它。 I had to add this line of code to my controller
我必须将这行代码添加到我的控制器中
ModelState.Clear();
My controller looks like this 我的控制器看起来像这样
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(int id, ViewModelCapsule model)
{
try
{
if(model.FirstModel != null && model.FirstModel.Menu1 != "ABC")
{
ModelState.Clear();
model.SecondModel = null
}
if (TryValidateModel(model))
{
// Here I am expecting the form to validate since the first form has valid date
// Do somethig with the request
}
return new RedirectAction("Index");
}
catch (Exception exception)
{
return Content(exception.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.