The bug about the htmlhelper method of checkbox in ASP. net mvc P5.

Source: Internet
Author: User

In ASP. net mvc P5, when you use this method to output checkbox: <% = html. checkbox ("checktest") %>, in addition to what you expect to see in the browserCodeIn addition, there is a hidden domain with the same name and checkbox. This leads to a problem: when the form is submitted, hidden (name = "checktest", value = "true") and input: checkbox (name = "checktest ", value = "false") is submitted at the same time, and the hidden domain is mandatory. As a result, in the server request, you get the request. form ["checktest"] may be like this: "false, true ". Therefore, when using HTML. checkbox () in P5, you must note this bug. The solution is simple: do not use it.

However, if you still want to use htmlhelper to get lazy, here I provide. helper method is compatible with the extension, and it is better than the official method -- it is bound with the label.

In fact, there is no difficulty. It is the same as other htmlhelper extensions. The basic implementation is provided here:

 

Code
  Public   Static   Class Checkboxextensions
{
Public   Static   String Checkbox ( This Htmlhelper helper, String Name, String Text, String Value, Bool Ischecked)
{
Return Checkbox (helper, name, text, value, ischecked, Null );
}

Public   Static   String Checkbox ( This Htmlhelper helper, String Name, String Text, String Value, Bool Ischecked, Object Htmlattributes)
{
VaR sethash = Htmlattributes. toattributelist ();

StringAttributelist= String. Empty;
If(Sethash! = Null)
Attributelist=Sethash;

Return   String . Format ( " <Input id = \ " { 0 }\ " Name = \ " { 0 }\ " Value = \ " { 1 }\ " {2} type = \ " Checkbox \ " {4}/> {3} " ,
Name, value, ischecked ?   " Checked = \ " Checked \ "" : "" ,
String . Isnullorempty (text) ?   "" : String . Format ( " <Label id = \ " { 0 }\ " For = \ " { 1 }\ " >{2 }</label> " , Name +   " _ Label " , Name, text ),
Attributelist );
}
}

 

If you have carefully read the P5 source code, you will find that this problem may not only occur in the checkbox, but is extended to the input tag. Write this articleArticleThe purpose is to pay attention to this bug and improve your extension methods.

 

Add:

VaR sethash = htmlattributes in the above Code. in toattributelist ();, the toattributelist () extension method may fail to be compiled by friends who directly copy the code because this method exists in ASP. net MVC toolkit seems to have been deleted, but this method is indeed very practical. I also sent this toattributelist () code:

 

Code
///   <Summary>
/// Creates a simple {0} = '{1}' list based on current object state.
///   </Summary>
Public   Static   String Toattributelist ( This   Object O ){
Stringbuilder sb =   New Stringbuilder ();
If (O ! =   Null ){
Hashtable attributehash = Getpropertyhash (O );

String Resultformat =   " {0} = \ " { 1 }\ "" ;
Foreach ( String Attribute In Attributehash. Keys ){
SB. appendformat (resultformat, attribute. Replace ( " _ " , "" ), Attributehash [attribute]);
}
}
Return SB. tostring ();

}

///   <Summary>
/// Creates a simple {0} = '{1}' list based on current object state. ignores the passed-in string [] items
///   </Summary>
///   <Param name = "O"> </param>
///   <Param name = "ignorelist"> </param>
///   <Returns> </returns>
Public   Static   String Toattributelist ( This   Object O, Params   Object [] Ignorelist ){
Hashtable attributehash = Getpropertyhash (O );

String Resultformat =   " {0} = \ " { 1 }\ "   " ;
Stringbuilder sb =   New Stringbuilder ();
Foreach ( String Attribute In Attributehash. Keys ){
If ( ! Ignorelist. Contains (attribute )){
SB. appendformat (resultformat, attribute, attributehash [attribute]);
}
}
Return SB. tostring ();

}

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.