Mvc3 (4)

Source: Internet
Author: User

Excerpt from the Book Pro ASP. NET MVC3 Framework:

Controllers and Actions

1. The Actions method cannot contain the out/ref parameter. ASP. net mvc will throw an exception in this case.

2. Optional/required Parameter

A. When the mvc framework cannot assign values to a parameter of the reference type, this action method will still be called. The mvc framework assigns null to this parameter.
B. When the mvc framework cannot assign values to a value type parameter, this action method will not be called and an exception will be thrown.
We can impose restrictions on these two situations:
A. the reference type is optional, that is, no value can be assigned. To avoid null value of the parameter, you can add the Code [add to the beginning] In this action Method for judgment and then perform corresponding processing, for example, if it is null, the program throws an exception.
B. The value type is required. To prevent exceptions, You can first assign a default value to this parameter or declare it as a null type [Nullable].
Format: int? Myvalue;
// In this way, myvalue can receive null values. [null values are not assigned]

3. View method in Action

The View method is used to form Html fragments and return them to the browser.

A. specifying a view by its path

You can directly display the view in the specified path, so that the mvc framework does not need to be searched again.

Public class ExampleController: Controller {
Public ViewResult Index (){
Return View ("~ /Views/Other/Index. cshtml ");
}
}

// When you specify a view like this, the path must begin with/or ~ /And include the file name extension

B. Specify the view name directly.

Public class ExampleController: Controller {
Public ViewResult Index (){
Return View ("Homepage"); // display the Homepage view
}
}

C. You can leave the view name unspecified.

Public class ExampleController: Controller {
Public ViewResult Index (){
Return View (); // The MVC Framework displays the Index view.
}
}

D. Pass a value to the view through Action

1) View Model Object
Public ViewResult Index (){
DateTime date = DateTime. Now;
Return View (date );
}

// Here, the DateTime object is passed as a view model, so that you can access it by using the "Model" keyword in Razor in the view.

The view section is as follows:

@ {ViewBag. Title = "Index ";}

<H2> Index

The day is: @ (DateTime) Model ). dayOfWeek) // This is a weak type, and the view does not know which type is passed. We can tell which type the view is, that is, create a strong type, as shown below:

Strong type View:
@ Model DateTime // specify the Model type
@{
ViewBag. Title = "Index ";
}
<H2> Index The day is: @ Model. DayOfWeek // The Model does not need to be converted.

4. Action can be passed through ViewBag

ViewBag can be used to set and assign values to any attribute, and then obtain the attribute value on the page.

Public ViewResult Index (){
ViewBag. Message = "Hello ";
ViewBag. Date = DateTime. Now;
Return View ();
}

The view section is as follows:
@{
ViewBag. Title = "Index ";
}
<H2> Index The day is: @ ViewBag. Date. DayOfWeek
</P>
The message is: @ ViewBag. Message

5. Action can pass values through View Data

Public ViewResult Index (){
ViewData ["Message"] = "Hello ";
ViewData ["Date"] = DateTime. Now;
Return View ();
}

The view section is as follows:
The day is: @ (DateTime) ViewData ["Date"]). DayOfWeek)
<P/>
The message is: @ ViewData ["Message"]
--- ViewBag is only available in MVC3. ViewData is used in the past to be equivalent to the View Model method,
The ViewData feature is similar to ViewBag, but it is implemented using the ViewDataDictionary class, while ViewBag adopts the dynamic object method.

6. Action can be passed through TempData

Public ViewResult Index (){
TempData ["Message"] = "Hello ";
Return View ();
}

The view section is as follows:
The Message is: @ TempData ["Message"]

The differences between ViewBag, ViewData, and TempData are recorded at http://www.cnblogs.com/notebook2011/archive/2012/11/29/2794478.html.

7. Redirect

1) There are two types of redirects: a. 301 [permanent redirection]: when the search engine captures new content, it also replaces the old URL with the redirected URL.

B. 302 [temporary redirection]: The Search Engine captures new content and retains the old URL. Because the server returns code 302, the search engine considers that the new website is only temporary.

Temporary jump Redirect [302 ]:

Public RedirectResult Redirect (){
Return Redirect ("/Example/Index ");
}

Permanent jump RedirectPermanent [301 ]:

Public RedirectResult Redirect (){
Return RedirectPermanent ("/Example/Index ");
}

2) There are two alternative methods:

RedirectToRoute [302 ]:

Public RedirectToRouteResult Redirect (){
Return RedirectToRoute (new {
Controller = "Example ",
Action = "Index ",
ID = "MyID"
});
}

RedirectToRoutePermanent [301 ]:

Public RedirectToRouteResult Redirect (){
Return RedirectToRoutePermanent (new {
Controller = "Example ",
Action = "Index ",
ID = "MyID"
});
}

3) Jump to an action

Public RedirectToRouteResult Redirect (){
Return RedirectToAction ("Index ");
}

// If only the action name is specified and no controller is specified, the current controller is used by default.

// If you want to redirect to another controller, you need to provide the name as a parameter, like this:

Return RedirectToAction ("Index", "MyController ");

// RedirectToAction is a temporary jump; RedirectToActionPermanent is a permanent jump

 

 

 

 

 

 

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.