In MVC projects, when JQuery and $. Post are used to submit data, Chinese garbled characters are generated?
Solution:
Data encoding during $. post, using the escape method
$. Post ("@ Url. Action (" AddFriendLink "," Setup ")", {"Name": escape (name)}, function (data ){
| The code is as follows: |
Copy code |
|
If (data> 0 ){
Alert ('added successfully! ');
Window. location. reload ();
}
Else {
Alert ('add failed! ');
}
}); |
DeCODE in the background and use the Server. UrlDecode method.
| The code is as follows: |
Copy code |
|
Public JsonResult Add (string Name)
{
DemoClass demoClass = newDemoClass
{
Name = Server. UrlDecode (Name)
};
Int result = demoService. Add (demoClass );
Return Json (result );
} |
--------------------------------------------------------------------------------
2. How to set the text box width in MVC?
In fact, this is not a problem, but it was still wrong at the beginning.
At the beginning, I wrote:
| The code is as follows: |
Copy code |
| @ Html. TextBoxFor (model => model. Name, new {width = "300px"}) (×) |
No effect was found.
The second solution was implemented.
| The code is as follows: |
Copy code |
| @ Html. TextBoxFor (model => model. Name, new {@ style = "width: 300px"}) (& radic ;) |
--------------------------------------------------------------------------------
3. How to use Html. RadioButtonFor?
As shown below. When Model. IsStudent = true, the first item is automatically selected, and so on.
| The code is as follows: |
Copy code |
@ Html. RadioButtonFor (model => model. IsStudent, true) Yes
@ Html. RadioButtonFor (model => model. IsStudent, false) No |
--------------------------------------------------------------------------------
4. @ helper custom method
| The code is as follows: |
Copy code |
|
@ Helper GetStatusName (int status)
{
// Code block
} |
You can directly use @ GetStatusName (2) for calling.
--------------------------------------------------------------------------------
5. When the model is referenced at multiple levels, what is the corresponding html?
| The code is as follows: |
Copy code |
| @ Html. TextBoxFor (model => model. Topic. Title) |
The generated HMTL code is:
| The code is as follows: |
Copy code |
| <Input name = "Topic. Title" class = "ConInpon" id = "Topic_Title" Onkeyup = "getLen (this)" type = "text"/> |
Pay attention when writing scripts.
--------------------------------------------------------------------------------
6. What are the common data binding methods for the DropDowlList control?
① The options in the drop-down box are a few simple fixed values.
| The code is as follows: |
Copy code |
@ Html. DropDownList ("ProvinceId", new SelectListItem [] {
New SelectListItem {Text = "select Category", Value = "0", Selected = true },
New SelectListItem {Text = "Category A", Value = "1 "},
New SelectListItem {Text = "Class B", Value = "2 "}
}) |
② Encapsulate the data as SelectList type and put it in ViewBag for transmission.
Controller layer:
| The code is as follows: |
Copy code |
|
IList <Person> personList = personService. GetList ();
ViewBag. personId = new SelectList (personList, "Id", "Name", queryParam. EditorId ); |
View layer: bind the View layer directly based on the ID.
@ Html. DropDownList ("personId ")
③ If there is a large amount of data on the page, it will be messy to pass them directly in viewbag. We can encapsulate the data needed in the page into a class, where the Select type is encapsulated as an attribute.
Encapsulation class
| The code is as follows: |
Copy code |
|
Public class IndexView
{
Public SelectList PersonList {get; set ;}
...
}
Controller: assign an initial value to the attribute in IndexView.
IList <Person> personList = personService. GetList ();
IndexView indexView = new IndexView ()
{
PersonList = new SelectList (personList, "Id", "Name", personList)
};
ViewBag. indexView = indexView;
View:
@ Html. DropDownListFor (x => queryParam. PersonId, indexView. PersonList) |
--------------------------------------------------------------------------------
7. Remember Ajax. BeginForm when submitting forms asynchronously!
When submitting a form, follow the following tips:
At the beginning, I wanted to use @ Html. BeginForm () + MVC3 for verification, but I wanted to asynchronously prompt the results.
If you use the. post method in Jquery for submission, you have to add the verification to the client script.
Then I suddenly remembered the Ajax. BeginForm () method that I forgot. It meets my needs. (* ^__ ^ *) Xi ......
| The code is as follows: |
Copy code |
|
@ Using (Ajax. BeginForm ("Index", "Manage", new AjaxOptions
{
HttpMethod = "Post ",
OnSuccess = "Success ",
OnFailure = "Failure"
}))
{
@ Html. ValidationSummary (true)
@ Html. TextBoxFor (model => model. Name, new {@ style = "width: 300px "})
@ Html. ValidationMessageFor (model => model. Name)
}
//// The script prompts the execution result
<Script language = "javascript" type = "text/javascript">
Function Success (){
Alert ("modified successfully ");
}
Function Failure (){
Alert ("modification failed! ");
}
</Script> |
You can also set the UpdateTargetId attribute to display the execution result after the execution result.
It is important to add
| The code is as follows: |
Copy code |
| <Script src = "@ Url. Content ("~ /Scripts/jquery. Unobtrusive-ajax.min.js ")" type = "text/javascript"> </script> |
Now it is basically successful.
Question:
Another problem is Chinese garbled characters? No problem with Html. BeginForm. Garbled characters will appear when Ajax. BeginForm is used. (Blog address)
Finally, I found the cause:
The view conflicts with the encoding in web. config:
| The code is as follows: |
Copy code |
|
<Script src = "@ Url. Content ("~ /Scripts/jquery. Unobtrusive-ajax.min.js ")" type = "text/javascript"> </script>
Web. config:
<Globalization fileEncoding = "gb2312" requestEncoding = "gb2312" responseEncoding = "gb2312" culture = "zh-CN"/> |
I also hope that you can provide a good solution generously. Here is the test project.
Here I will summarize the common problems I have encountered in mvc development. If you have more information, please comment below.