Summary of common MVC problems in asp.net

Source: Internet
Author: User

This article has added a large number of solutions to common MVC problems. For more information, see.

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"}) (√)


--------------------------------------------------------------------------------
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.

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.