When `HttpGet` processes the` Create` action method, it simply returns `View ()`, why doesn’t the javascript check run in the browser?

When HttpGetprocessing Createan action method simply returns View(), it causes all form fields to be uninitialized.

Why doesn't javascript validation run when users view a blank form?

+3
source share
4 answers

I do not know how this works, but I will give him guesses; -)

First of all, it has nothing to do with whether [HttpGet]or [HttpPost]not it usually works.

Your middle methods [HttpGet]look something like this:

[HttpGet]
public ActionResult Index() {
    // Get some data from the database
    return View(data);
}

, , , URL (.. Home/Index).

[HttpPost] :

[HttpPost]
public ActionResult Create(MyModel myModel) {
    if (ModelState.IsValid)
    {
         // Do stuff
    }
    else
    {
        return View(myModel);
    }
}

, , Create (...). [HttpGet] , .

, - ValidationMessageFor(m => m.SomeProperty) , . , .

, View() ActionResult ( HTTP GET), , . , POCO (Plain Old # Object) - .

, JavaScript . [HttpGet] , ( JavaScript ). , , , , JavaScript, change, keydown, keypress ..

+3

, ModelState , ValidationMessageFor ModelState.Errors, .

, , , , .

+2

Rails , . , .

+1

?

@{ Html.EnableClientValidation(); }

config

<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Javascript validation attaches to the submit event for the form. Downloading an empty form does not trigger this event in the user's browser.

0
source

Source: https://habr.com/ru/post/1781353/


All Articles