Home >Backend Development >C#.Net Tutorial >Example analysis of parameter transfer between MVC pages

Example analysis of parameter transfer between MVC pages

Y2J
Y2JOriginal
2017-04-27 11:12:243408browse

This article mainly introduces the method of parameter transfer between MVC pages in detail, and shares two ways of transferring values ​​between MVC pages. One is the Html.RenderPartial method, and the second is the Html.RenderAction method, which has certain advantages. For reference value, interested friends can refer to

Regarding value transfer between MVC pages, there are many ways. Below, we will share with you the Html.RenderAction method and Html.RenderPartial method. what is the difference.

1. Html.RenderAction method How to pass parameters between pages:

Html.RenderAction method is to access the Controller of another page and pass the parameters in Parameters in the corresponding Action in the Controller on another page.

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

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

In this Index, write the received parameters to ViewBag.content, and in the front-end View interface Obtained. As follows:

#So how do we use the Html.RenderAction method to pass parameters into this page and then display them?

This uses the Html.RenderAction method to pass parameters to the Index page in Top.

Have you all learned it?

2. Html.RenderPartial method

Html.RenderPartial method is different from Html.RenderAction. It does not pass parameters to the Action corresponding to the Controller of another page, but It is passed directly to the Model of another interface.

So, the value passed by Html.RenderAction will not directly enter the Model through the Controller.

Let’s take a look at the specific operations.

1. First, we create a simple Model model.

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

2. Create an interface Boot page, (named by Boot itself). Its View is as follows:

In the above code, we formulate the model of the page Not Person class. Get the corresponding value based on the attributes of the class in the two c1a436a314ed609750bd7c7d319db4da tags.

After creating this page, we have to pass the value to this page. So how to use Html.RenderPartial to pass the value to it? As follows:

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

This method seems to have some shortcomings. Yes, it is impossible for us to build a Model every time we use Html.RenderPartial to pass a value. This would be almost a disaster. So how to avoid it?

The ideal way for us to use Html.RenderPartial to pass values ​​to another interface may be like this:

As shown in the red box above. Just use an anonymous object to wrap the parameters directly, and then pass it over, but will another page accept it?

Perhaps, everyone has already tested it, and the answer is: no. If you go to Name and Sex in Model, an error will be reported (I believe all garden friends have already tried it)

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

#If you use the above method to get the value, you will get the corresponding attributes of the passed anonymous object. Isn't it very high? It will be much more convenient to pass values ​​in the future.

The types of parameters passed can also be of various types, for example:

We added a DateTime type parameter, then the other interface is still the same , the answer is correct.

Page

Okay, let’s share here about the parameter transfer between MVC pages.

The above is the detailed content of Example analysis of parameter transfer between MVC pages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn