A radical solution for some bugs found in the MVC Toolkit Part (1)

Source: Internet
Author: User

Here I only talk about MVC Toolkit.
First, because MVC Toolkit does not have many bugs, and second, because I have made a lot of comments on MVC and WebForm, some may misunderstand my views on MVC and WebForm. (In fact, I think they have their own advantages, I didn't mean anything bad, But TNT2 would do what I found out, even if it was made. By the way, I would like to thank my friends who have come together with me !).

Let's get down to the truth.

Time relationship. Here we only talk about a bug that gave me a headache for one night.
If you use Html. select () is a strange phenomenon: not all provided rewrite schemes provide htmlAttributes (used to input attributes such as onclick ), only one (6 of 6) is provided. In any case, we are more tolerant. We can use htmlAttributes to finish the first five items. In this field, I entered the code like this:

New {onchange = "checkMiniCycle ();"}


Clearly, I need to execute the checkMiniCycle () Client js command when the select option is changed.
The result didn't respond. I read the HTML source code of the client. Of course, I want to see the following section in the select statement: onchange = "checkMiniCycle ();". Instead, IsReadOnly = "False" IsFixedSize = "False" IsSynchronized = "False" Keys = "System. collections. hashtable + KeyCollection "Count =" 1 "Values =" System. collections. hashtable + ValueCollection "SyncRoot =" System. object"

PS: To facilitate your understanding, I will only extract the corresponding section. In fact, at the beginning, I didn't know that the output was wrong. I thought it was the code automatically generated by Html. Select () Like the server space, but my onclick didn't output it. By adding parameters and other tests, I found that Count = "1" (marked in red above) increased, so I determined through various indications that it outputs the entire htmlAttributes (actually a HashTable) instead of htmlAttributes. It took me half a day.

So I Reflect MVCToolkit. dll (the same is true for source code ):

The code for the sixth method of Html. Select () is found:

As with the source code, I will release the source code (with comments ):

1/** // <summary>
2 // Creates an HTML Select drop-down based on the passed-in datasource.
3 /// </summary>
4 // <param name = "dataSource"> IEnumerable, IQueryable, DataSet, DataTable, or IDataReader </param>
5 // <param name = "htmlName"> The name of the control for the page </param>
6 /// <param name = "textField"> The datasource field to use for the display text </param>
7 // <param name = "valueField"> The datasource field to use for the control value </param>
8 /// <param name = "selectedValue"> The value that shocould be selected </param>
9 // <param name = "htmlAttributes"> Any attributes you want set on the select tag. Use anonymous-type declaration for this: new {class = cssclass} </param>
10 public static string Select (this HtmlHelper helper, string htmlName, object dataSource, string textField,
11 string valueField, object selectedValue, int size, bool multiple, object htmlAttributes ){
12 // input formats
13 string selectFormat = "\ t <select name = \" {0} \ "id = \" {0} \ "{1}>" + Environment. newLine + "{2}" + Environment. newLine + "\ t </select> ";
14 string optionFormat = "\ t <option value = \" {0 }\ "{1 }>{ 2} </option>" + Environment. NewLine;
15
16 Hashtable setHash = HtmlExtensionUtility. GetPropertyHash (htmlAttributes );
17
18 // output
19 StringBuilder sbOptions = new StringBuilder ();
20 DataTable tbl = MvcControlDataBinder. EvalDataSource (dataSource, textField, valueField );
21
22 // loop the source
23 foreach (DataRow dr in tbl. Rows ){
24 string selectedFlag = string. Empty;
25 string thisText = dr [1]. ToString ();
26 string thisValue = dr [0]. ToString ();
27
28 if (selectedValue! = Null ){
29 if (HtmlExtensionUtility. AreEqual (selectedValue, dr [0])
30 selectedFlag = "selected = \" true \"";
31}
32 sbOptions. AppendFormat (optionFormat, helper. Encode (thisValue. ToString (), selectedFlag, helper. Encode (thisText ));
33}
34
35 string attributeList = string. Empty;
36 if (setHash! = Null)
37 attributeList = setHash. ToAttributeList ();
38
39 if (multiple)
40 attributeList + = "multiple ";
41
42 if (size> 0)
43 attributeList + = "size =" + size. ToString ();
44
45 return string. Format (selectFormat, htmlName, attributeList, sbOptions. ToString ());
46}
47

The problem lies in the two red lines above, and I don't know if it's my machine problem (not likely) or the same is true for everyone. I output these directly in aspx, it's also a mess of code. So I confirmed the problem.

I changed these two lines to the following:

Var setHash = htmlAttributes. ToAttributeList ();

AttributeList = setHash;

PS: I did not test the above if to determine whether it is redundant or whether there is a more efficient method. Here is only one solution.

Next, compile and overwrite the DLL in the project, re-compile and run the program. Finally succeeded!

I just gave this example because I am in this Html today. select () is stuck here, others such. textBox and so on may also have similar problems. A solution is provided here. We hope that you can skip this issue or use a similar method to correct it.

MVCToolkit. dll that has modified the Html. Select () attribute bug is provided here. Download: MVCToolkit.rar

Welcome to our additional discussion ^_^

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.