Talking about parameter transfer between MVC pages, talking about mvc page Transfer

Source: Internet
Author: User

Talking about parameter transfer between MVC pages, talking about mvc page Transfer

There are multiple methods for passing values between MVC pages. Let's share the differences between Html. RenderAction and Html. RenderPartial.

I. Html. RenderAction:

The Html. RenderAction method is to access the Controller of another page, and pass the parameters to the parameters in the corresponding Action in the Controller of the other page.

For example, I have such a Controller and Action (a partial view)

1     public class TopController : Controller2     {3         // GET: Top4         public ActionResult Index(string content)5         {6             ViewBag.content = content;7             return PartialView();8         }9     }

In this Index, the received parameters are written to ViewBag. content and obtained from one front-end View interface. As follows:

So how can we use Html. RenderAction to pass Parameters to this page and display them?

This is to use the Html. RenderAction method to pass the parameter to the Index page in the Top.

What have you learned?

Ii. Html. RenderPartial Mode

The Html. RenderPartial method is different from the Html. RenderAction method. It does not pass the parameter to the Action corresponding to the Controller on the other page, but directly to the Model on the other interface.

Therefore, the value of Html. RenderPartial does not go through the Controller to directly enter the Model.

Next, let's take a look at the specific operations.

1. First, we will resume a simple Model.

1     public class Person2     {3         public string Name { get; set; }4         public string Sex { get; set; }5      6     }

2. Create the Boot page (the name obtained by the Boot user). The View is as follows:

In the code above, we set the model of the page to a non-Person class. In the two

After this page is created, we need to pass a value to this page. How can we use Html. RenderPartial to transfer the value to it? As follows:

The above red box uses Html. RenderPartial to pass the value.

There seems to be some shortcomings in this method. Yes, it is impossible for us to create a Model every time we use the Html. RenderPartial method to pass values. This is almost a disaster. So how to avoid it?

The ideal method for passing values to another interface using Html. RenderPartial is as follows:

As shown in the red box above. Directly use an anonymous object to package the parameters and pass them in. However, will the other page accept them?

Maybe you have already tested it. The answer is: no. If you go to Name and Sex in the Model, an error will be reported (I believe you have already done this experiment)

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

If the value is set in the preceding method, the attributes of the passed anonymous object are obtained. Is it very high? It will be much more convenient to pass the value in the future.

The passed parameters can also be of various types, such:

We have added a DateTime parameter, so the other interface is still the same.

Page

Now, we will share the parameter transfer between MVC pages.

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.