Two minor problems in the aspnet mvc Framework

Source: Internet
Author: User

The MVC framework has a very good function, that is, after the form is submitted, the entity can be automatically assigned a value, as long as the attribute name and the HTML element name are the same, it can basically bind and modify Common Object content, and also has the automatic form error verification function. Such a powerful function should make development more efficient, but it is still a little troublesome to use.

In the MVC framework, the main call Method for automatically assigning form values to entities is tryupdatemodel, and the key helper class is formcollection. Most of the work is completed in formcollection. The formcollection object passed to the contoller method is automatically generated by the MVC framework. When an object is assigned a value, values in the corresponding formcollection are obtained through the object attribute name. An exception is thrown when the value of formcollection is extracted. See the following exception information:

 

Trace

System. Web. MVC. formcollection. getvalue (string name) + 278515
System. Web. MVC. formcollection. system. Web. MVC. ivalueprovider. getvalue (string key) + 4
System. Web. MVC. defaultmodelbinder. bindmodel (controllercontext, modelbindingcontext bindingcontext) + 219
System. Web. MVC. Controller. tryupdatemodel (tModel model, string prefix, string [] includeproperties, string [] excludeproperties, ivalueprovider valueprovider) + 449
System. Web. MVC. Controller. tryupdatemodel (tModel model, ivalueprovider valueprovider) + 56

 

 

 

According to the exception information, it can be determined that an NUL value is input when the getvalue method is called, so an error parameter exception is thrown. At first, I suspected that I didn't have the permission to access the attribute during reflection. As a result, I returned an empty property name and tried to add permission control. I also carefully checked what I wrote.CodeNo difference was found between the codes in <pro ASP. net mvc Framework>. The only difference is that the MVC version I use is RC2, and the book uses the RC1 version. It is estimated that the new version is not supported.

Finally, I searched the internet and the results almost jumped up. This is a confirmed bug in RC2. The solution is to modifySource code, Re-compile a DLL. The modified code is also very simple. Remove string. isnullorempty () from formcollection. getvalue.

I thought it could run smoothly, but another error occurred. The value returned from HTML. checkbox ("CHB") cannot be converted to the bool type normally, and an exception of type conversion failure is returned each time. After tracking and debugging, it is found that the string passed from form is "True, false". It is no wonder that the conversion will fail. The question is, why is this value returned? There is only one checkbox control on the interface. Check the HTML source code of the page and find that there is an extra hidden field with the same name as the checkbox, and the value is false. Therefore, after each submission, two values "True, false" are returned ". Is it a framework issue? After searching, we found that the error still occurred in the framework. The explanation provided on the Internet is that "True, false" is returned during the election, and "false" is returned when no selection is made ". So I tried it. This problem can also be solved by modifying the Framework Code, customizing an htmlhelper extension method to generate a checkbox, or taking the first word when taking the checkbox value. I use the last method.

After solving the above two problems, we were able to run smoothly. It seems that open-source frameworks are not completely dependent on each other. However, the MVC framework is more advantageous than the classic ASPnet framework.

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.