Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the parameter transfer between MVC pages

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "parameter transfer between MVC pages how to achieve", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "parameter transfer between MVC pages how to achieve" this article.

1. Html.RenderAction method: how to transfer parameters between pages:

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

For example: I have such a Controller and Action (which is a partial view)

Public class TopController: Controller {/ / GET: Top public ActionResult Index (string content) {ViewBag.content = content; return PartialView ();}}

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

So how do we use Html.RenderAction to pass parameters to this page and display them?

This is how parameters are passed to the Index page in Top using Html.RenderAction.

II. Html.RenderPartial mode

Html.RenderPartial differs from Html.RenderAction in that it does not pass parameters to the Action corresponding to the Controller of another page, but directly to the Model of another interface.

Therefore, the value passed by Html.RenderAction does not enter Model directly through Controller.

Let's take a look at the specific operation.

1. First of all, let's resume a simple Model model.

Public class Person {public string Name {get; set;} public string Sex {get; set;}}

2. Create the Boot page of the interface. (Boot chose its own name) and its View is as follows:

In the above code, we define the model of the page as a non-Person class. In the two tags, the corresponding values are taken according to the attributes of the class.

After building this page, we are about to pass a value to this page, so how to use Html.RenderPartial to pass a value to it? As follows:

In the above red box, the value is passed by Html.RenderPartial.

There seems to be some inadequacies in this approach, yes, it is impossible for us to build a Model model every time we pass values in Html.RenderPartial, which is almost a disaster. So how to avoid it?

The ideal way for us to pass values to another interface in Html.RenderPartial way might be like this:

As shown in the red box above. Just wrap the parameter in an anonymous object and pass it to it, but will the other page accept it?

Maybe everyone has already tested it, and the answer is: no. If you go to Name and Sex in Model, you will report an error (I believe you have already tried it).

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

If you take the value in the above way, you will get the corresponding properties of the passed anonymous object. Is it very high? it will be much more convenient to pass the value later.

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

If we add a parameter of type DateTime, the other interface is still the same.

Page

The above is all the content of the article "how to transfer parameters between MVC pages". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report