Hello. There is such a question on ASP.NET MVC. Here is my view:

@model MvcApplication4.Models.Class1 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> @Html.EditorFor(x => x.Stroka1) @Html.EditorFor(x => x.Stroka2) @Html.EditorFor(x => x.Stroka3) @Html.DisplayFor(x => x.Stroka1)<br /> @Html.DisplayFor(x => x.Stroka2)<br /> @Html.DisplayFor(x => x.Stroka3)<br /> </div> </body> </html> 

Here is the action method:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication4.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index(string Stroka1, string Stroka2, string Stroka3) { return View(); } } } 

Well, the model itself:

 public class Class1 { public string Stroka1 { get; set; } public string Stroka2 { get; set; } public string Stroka3 { get; set; } } 

The question is why, when requesting this type:

http: // localhost: 5669 / home / index? Stroka1 = Pavel & Stroka2 = Gleb & Stroka3 = Sergey

only the Html.EditorFor view fields are Html.EditorFor , and Html.DisplayFor displayed empty. As far as I understand, when requesting this type, an object of the Class1 model is Class1 and accordingly everything should be displayed. But only input fields are displayed, but not display. Where am I wrong, please tell me? On the other hand, in the Index action method, no model is passed to the View() method at all, then it turns out that the display should be completely without this data from the query. But for some reason, everything happens unexpectedly.

    2 answers 2

    EditorFor conceived primarily for editing an existing model. Onn searches for the value first in ViewData.ModelState , then in the model itself. This is done for the case when

    • you displayed a form
    • The user entered the data - sent from in the get-line or in the body of the request
    • the data has not been validated on the server (and is now in ModelState , but not in the model - because it may not be completely valid for it - for example, strings that cannot be parsed into the integer model fields)
    • you display the form as the result of an action

    In this case, the user should see the data entered by him from the ModelState — values ​​from the request body or from the get parameters. Therefore, EditorFor first applies the values ​​from ModelState (from the query), and then from the model. The model itself is not created in this case - an instance of class Class1 is not created.

    One of the not very obvious consequences of such behavior - EditorFor will show the values ​​transferred from the client to the controller even if these values ​​are empty in the model. And even if you did not transmit the model in View() . To explicitly "emit" values ​​from ModelState , call ModelState.Remove :

     public ActionResult Index(string Stroka1) { ModelState.Remove("Stroka1"); return View(); // в этом View EditorFor(m => m.Stroka1) покажет пустой текстбокс } 

    DisplayFor intended only to display data. For him, the tricky steps of getting data from ModelState , and not from the model, are not out of date. Therefore, it simply takes the values ​​from the model that you have is empty. Fill it in, and both EditorFor and DisplayFor will work correctly for you:

     public ActionResult Index(string Stroka1, string Stroka2, string Stroka3) { return View(new Class1 { Stroka1 = Stroka1, Stroka2 = Stroka2, Stroka3 = Stroka3 } ); } 
    • Thanks, it seems so clearer !! That is, the point in my particular case is in particular the operation of the methods of Edith and Display? - Polyakov Sergey
    • one
      @polyakov_s yes. rather, it is a feature of the work of the EditorFor method for organizing server validation. - PashaPash
    • one
      @polyakov_s It does not create Class1 . EditorFor takes the name of the property (this is a template method, in which the type from the @model directive is substituted). Searches for properties by name in ModelState . If found - shows it in the textbox. - PashaPash
    • I re-read your post, and saw! Thank you so much, otherwise I spent 2 hours of my life and could not understand anything. Now I entered the topic)) Thank you !! - Polyakov Sergey

    Maybe it makes sense for you to pass some model to your view? As far as can be judged here

     public ActionResult Index(string Stroka1, string Stroka2, string Stroka3) { return View(); } 

    you have no model, you just render an empty view. Or do you think that the model will magically be generated by itself?

    • I fully agree with you, but this link andreyrey.moveax.ru/post/mvc3-in-depth-part23-model-binding in the first part says that the model is already created. And supposedly goes to the view. And besides, the question then torments me, but why then the input fields are filled with data from this link (which is in my question)? After all, in theory, then all the data transmitted by reference should not be displayed at all in any way if the model is empty - Polyakov Sergey
    • one
      @polyakov_s is not quite on topic - but the code on the link uses TempData -> uses the session -> potentially causing performance problems. Find another tutorial. - PashaPash
    • Thank you very much, I will look for another then. I got into it and stuck to my ears - Polyakov Sergey