Is this a big bug for ASP.

Source: Internet
Author: User
Tags string format

This is a colleague's problem yesterday, I think this is a pretty big problem, and should not be the intent of ASP. NET MVC designers, in other words, this may be a big bug in ASP. StackOverflow also has a description of the problem http://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear

With less gossip, we re-issue this problem through a simple question. First we define the following default HomeController, which has a default action method of index. The method takes a parameter of type Demomodel, and the logic in it is very simple: we have slightly modified the three properties of the parameter to render it as model in the corresponding view.

 Public classhomecontroller:controller{ PublicActionResult Index (Demomodel model) {model. Foo+=": Changed"; Model. Bar+=": Changed"; Model. Baz+=": Changed"; returnView ("Index", mode); }} Public classdemomodel{ Public stringFoo {Get;Set; }  Public stringBar {Get;Set; }  Public stringBaz {Get;Set; }}

For the view (index.cshtml) of the action method Index, we can present the model object in compile mode in the following three ways.

The first form of @model demomodel@html.labelfor (M=>m.foo) @Html. textboxfor (M = m.foo) @Html. Labelfor (M + m.bar) @ Html.textboxfor (M = m.bar) @Html. labelfor (M = M.baz) @Html. textboxfor (M = m.baz)//second form @model Demomodel@html.labelfor (M=>m.foo) @Html. editorfor (M = m.foo) @Html. labelfor (M = m.bar) @Html. editorfor (M = > M.bar) @Html. labelfor (M = M.baz) @Html. editorfor (M = m.baz)//Third form @model Demomodel@html.editorformodel

Now we run the program and provide the data as the Action Method index parameter (? foo=123&bar=456&baz=789) in the form of query string, and we can see that the original value is always present on the interface, This means that we did not have any effect on the modification of the original data in the action method index.

By looking at the code of the ASP. NET MVC framework itself, I think the root of the problem should originate from the InputExtensions type of Inputhelper method. As shown below, Inputhelper is fetched from the current modelstate when the specified form element is worth it, and if the value does not exist in Modelstate, it is obtained from the current viewdata. For this example, the value in Modelstate is the original value, and the value of ViewData takes the modified value.

Public static class inputextensions{private static Mvchtmlstring Inputhelper (HtmlHelper htmlhelper, InputType INP Uttype, Modelmetadata metadata, string name, object value, BOOL Useviewdata, BOOL isChecked, BOOL setId, BOOL Isexplicitva Lue, string format, IDictionary<string, Object>htmlattributes);} private static mvchtmlstring Inputhelper (HtmlHelper htmlhelper, InputType inputtype, modelmetadata metadata, string name , object value, BOOL Useviewdata, BOOL isChecked, BOOL setId, bool Isexplicitvalue, string format, IDictionary<string, Object>htmlattributes) {... switch (inputtype) {... default: { string str4 = (string) htmlhelper.getmod            Elstatevalue (Fullhtmlfieldname, typeof (String)); Tagbuilder.mergeattribute ("value", STR4??            (Useviewdata htmlhelper.evalstring (fullhtmlfieldname, format): str2), isexplicitvalue);        Goto label_016c; }} ...}

Is this a big bug for ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.