关闭Javascript时,ASP.NET MVC 4服务器验证不会出现

时间:2021-03-09 04:02:46

Have given all sorts of validations like [Required], [StringLength] etc..etc.. in my model class, but these server side validation messages only appear when Javascript is turned on in the browser.

在我的模型类中给出了[Required],[StringLength] etc..etc ..等各种验证,但这些服务器端验证消息仅在浏览器中打开Javascript时出现。

On disabling Javascript, none of these server side validations seem to work, i cant figure out why it is happening like this.

在禁用Javascript时,这些服务器端验证似乎都不起作用,我无法弄清楚为什么会发生这样的情况。

Can someone guide me on this issue.

有人可以指导我这个问题。

1 个解决方案

#1


0  

In your controller you can ask if the Model you are posting is valid with:

在您的控制器中,您可以询问您要发布的模型是否有效:

[HttpPost]
public ActionResult Index(MyModel model)
{
    if(ModelState.IsValid)
    {
        // some persistence logic
        return RedirectToAction("Index");
    }

    ViewBag.Error("there were some errors in your form.");

    return View(model);
}

To be clear, this behaviour server side is completely unaffected by the client's javascript setting. The client's setting only has the ability to allow or prevent form submission to your controller.

要明确的是,此行为服务器端完全不受客户端的javascript设置的影响。客户端的设置只能允许或阻止向您的控制器提交表单。

#1


0  

In your controller you can ask if the Model you are posting is valid with:

在您的控制器中,您可以询问您要发布的模型是否有效:

[HttpPost]
public ActionResult Index(MyModel model)
{
    if(ModelState.IsValid)
    {
        // some persistence logic
        return RedirectToAction("Index");
    }

    ViewBag.Error("there were some errors in your form.");

    return View(model);
}

To be clear, this behaviour server side is completely unaffected by the client's javascript setting. The client's setting only has the ability to allow or prevent form submission to your controller.

要明确的是,此行为服务器端完全不受客户端的javascript设置的影响。客户端的设置只能允许或阻止向您的控制器提交表单。