Instance parsing of parameter passing between MVC pages

Source: Internet
Author: User
This article mainly introduces the method of parameter transfer between MVC pages, and shares the two ways of transferring values between MVC pages, one is the html.renderpartial way, the other is the html.renderaction way, it has certain reference value, Interested in the small partners can refer to

There are several ways to transfer values between MVC pages, and below, we'll share what's different with html.renderaction and html.renderpartial ways.

One, the way of passing parameters between pages of Html.renderaction mode:

The Html.renderaction method is to access the controller of another page and pass the parameter to the parameter in the corresponding action in the controller of another page.

For example: I have such a controller and action (is a partial view)

public class Topcontroller:controller {  //get:top public  actionresult Index (string content)  {   viewbag.content = content;   return Partialview ();  } }

In this index, the parameters to be received are written to Viewbag.content, which is taken from the front-end view interface. As follows:

So how do we use the Html.renderaction method to pass parameters to this page and show them?

This is the use of the Html.renderaction method, passing parameters to the Index page in top.

Did everyone learn?

Second, the Html.renderpartial way

Unlike Html.renderaction, Html.renderpartial does not pass arguments to the controller corresponding to another page, but instead passes directly to the model of the other interface.

Therefore, the html.renderaction value will not go through the controller directly into the model.

Let's take a look at the specific operation.

1, first of all we resume a simple model.

public class person  {public   string Name {get; set;}   public string Sex {get; set;}    }

2, build the Interface boot page, (boot own name) its view is as follows:

In the above code, we make the model of the page the non-person class. In two

After setting up this page, we will pass the value to this page, then how to use html.renderpartial to pass value to it? As follows:

The above red box is the html.renderpartial way to pass the value.

There seems to be some inadequacy in this approach, and yes, we are not going to have to build a model every time we use the Html.renderpartial method, which is almost a disaster. So how to avoid it?

The ideal way for us to pass values to another interface in a html.renderpartial way might be this:

As shown in the red box above. The parameter is wrapped directly with an anonymous object and passed in the past, but will the other page accept it?

Perhaps, we have already tested, the answer is: No. In the model to go to name and sex, will be error (I believe you have already experimented with the garden friends)

So how do we improve, so that the page can accept the parameter passing in this way? As follows:

The above method to fetch the value, it will be passed to the anonymous object of the corresponding properties. is not very high Ah, later the value is more convenient.

The types of parameters passed can also be varied, such as:

We add a datetime type parameter, then the other interface is the same, the camera is not mistaken.

Page

Well, here's what you can do about the arguments passed between the MVC pages.

Related Article

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.