Return View detailed MVC

Source: Internet
Author: User

1, return View ();
return value
Type: System.Web.Mvc.ViewResult
Renders the view to the response's View() result.
NotesThis method overload of the View () class returns a ViewResult object with an empty ViewName property. If you are writing unit tests for controller actions, consider the empty ViewName property for unit tests that do not take a string view name.

At run time, if the ViewName property is empty, the ViewName property will be replaced with the current operation name.

2, return View (object  model);  
Parameters
Model
Type: System.Object
the model that the view renders.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

This overload of the View () method returns a ViewResult object that has an empty ViewName property. If you are writing unit tests for controller actions, consider the empty ViewName property for unit tests that do not take a string view name.

3, return View (string viewName);

Parameters
ViewName
Type: System.String
The name of the view to render for the response.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

When the result object prepared by this method is executed, the ASP. NET MVC Framework writes the object to the response.

4, return view (IView view);

Parameters
View
Type: System.Web.Mvc.IView
The view that is rendered in response.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

When the result object prepared by this method is executed, the ASP. NET MVC Framework writes the object to the response.

5 return View (string viewName, object model);

Parameters
ViewName
Type: System.String
The view that is rendered in response.
Model
Type: System.Object
the model that the view renders.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

When the result object prepared by this method is executed, the ASP. NET MVC Framework writes the object to the response.

6 return View (String viewName, string mastername);

Parameters
ViewName
Type: System.String
The name of the view to render for the response.
Mastername
Type: System.String
The name of the master page or template to use when rendering the view.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

When the result object prepared by this method is executed, the ASP. NET MVC Framework writes the object to the response.

7 Return view (IView view, object model); Parameters
View
Type: System.Web.Mvc.IView
The view that is rendered in response.
Model
Type: System.Object
the model that the view renders.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

When the result object prepared by this method is executed, the ASP. NET MVC Framework writes the object to the response

8return View (String viewName, String Mastername, object model);
Parameters
ViewName
Type: System.String
The name of the view to render for the response.
Mastername
Type: System.String
The name of the master page or template to use when rendering the view.
Model
Type: System.Object
the model that the view renders.
Return value type: System.Web.Mvc.ViewResult
view results. Notes

When the result object prepared by this method is executed, the ASP. NET MVC Framework writes the object to the response.

Show:InheritanceProtection
name description
Mastername Gets the name of the master view (such as a master page or template) to use when rendering the view.
Model Gets the view data model. (inherited from Viewresultbase.) )
TempData Gets or sets the Tempdatadictionary object for this result. (inherited from Viewresultbase.) )
View Gets or sets the IView object that is rendered to the response. (inherited from Viewresultbase.) )
ViewBag Gets the view package. (inherited from Viewresultbase.) )
ViewData Gets or sets the view data Viewdatadictionary object for this result. (inherited from Viewresultbase.) )
Viewenginecollection Gets or sets the collection of view engines associated with this result. (inherited from Viewresultbase.) )
ViewName Gets or sets the name of the view to render. (inherited from Viewresultbase.) )
  

The action we see above is the return View (), which we can consider as the return value used to parse an ASPX file. And its return type is actionresult as

      Public ActionResult Index ()        {            return View ();        }

Besides view () what value can we use here to return?

I. ASCX page

Scenario: To return a snippet, such as Ajax, to return a sub-page

Let's start with a new action

        Public ActionResult Ascx ()        {            return Partialview ();        }

We'll build a view below, still in action, right-click, AddView.

Note the picture is checked.

So I created an ascx page, and we're going to rewrite it a little bit.

<%@ Control language= "C #" inherits= "System.Web.Mvc.ViewUserControl"%><div> get a div</div>

Run, get the page

Second, return text

In addition to the above, we sometimes return only a piece of text.

At this point we can use the following action form:

        Public ActionResult text () {            return Content ("This is a piece of text");        }
Third, return JSON

Sometimes when we call Ajax we also ask that the returned object be the result of JSON serialization, such as:

        Public ActionResult Showjson ()        {            var m = new Eiceindexmodel            {                Name = "Kingjian",                Sex = True            };            Return Json (m);        }

Return text:

{"Name": "Kingjian", "Sex": true}
Iv. Output JS file

Most of the time JS files are static, but sometimes JS files may also be generated dynamically at this point we can output

        Public ActionResult Js ()        {            return JavaScript ("var x=0;");        }

We visit, get a normal page but its content-type:application/x-javascript; Charset=utf-8

Five, page jump

1. Jump to URL

        Public ActionResult Rdurl ()        {            return Redirect ("http://www.baidu.com");        }

2. Jump to Action

        Public ActionResult rdaction ()        {            return redirecttoaction ("Index", "Eice");        }

3. Jump to Routing Rule

        Public ActionResult rdrouting ()        {            return Redirecttoroute ("Default",//route name              new{                  Controller = "Eice ",                  Action =" Index "              });        
Vi. Display Files
        Public actionresult fn ()        {            return file (                "/content/site.css"//File path                , "text/css"//file type                );        }

Return View detailed MVC

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.