简体   繁体   English

使用ASP.NET MVC 5更改请求后,如何在控制器中手动验证模型?

[英]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.

相关问题 asp.net mvc如何将视图模型和输入文本传递给控制器​​? - asp.net mvc how can I pass the view model and input text to controller? 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 在成功向ASP.NET MVC控制器发送jQuery Ajax请求后,如何运行JavaScript函数? - How to run a JavaScript function after a successful jQuery Ajax request to ASP.NET MVC controller? 如何使用 asp.net mvc 验证错误消息? - How to validate error message using asp.net mvc? 在多模型 ASP.NET MVC 中验证单个模型 - Validate single Model in Multiple Model ASP.NET MVC 如何使用 MVC asp.net 从数据库中检查以逗号分隔的 model 中存在的复选框(如下面的代码所示)? - How can I check checkbox which is exists in model sepereted by coma from database (as shown below in code) using MVC asp.net? ASP.Net MVC中的简单Dropzone实现-如何在Controller中获取数据? - Simple Dropzone implementation in ASP.Net MVC - How can I get data in the Controller? 如何基于ASP.NET MVC中的控制器操作在页面javascript中设置变量? - How can I set a variable in my pages javascript based on my controller action in ASP.NET MVC? 如何将JavaScript文件路径映射到ASP.Net MVC控制器操作? - How can I map a JavaScript file path to ASP.Net MVC controller action? 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM