How to pass data from view to UserControl in ASP.NET MVC?

Let's say I want to make the simplest of the following data:

<% For i = 0 To 10%>
    <%Html.RenderPartial("MyUserControl")%>
<% Next%>

What I want to do is pass the variable i as a parameter to the UserControl so that it displays the number inside, say, the border div.

How is this possible?

thank

+3
source share
2 answers
<% For i = 0 To 10%> 
    <%Html.RenderPartial("MyUserControl", i)%> 
<% Next%>

The RenderPartial method has an overload that allows you to pass in a (sub) model. To use it most effectively, your UserControl must be strongly typed - in this case, to a model of type System.Int32.


To use it in a UserControl:

<%@ Control Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<System.Int32>" %>
<div><%= this.Html.Encode(this.Model) %></div>

In this case, it this.Modelis an instance of System.Int32.

+6
  • . , -.
  • PLease MVC v2;) .
-2

Source: https://habr.com/ru/post/1738324/


All Articles