[英]How to pass data from view to UserControl in ASP.NET MVC?
Say I want to do the simplest of the data passing as follows : 假设我想做最简单的数据传递,如下所示:
<% For i = 0 To 10%>
<%Html.RenderPartial("MyUserControl")%>
<% Next%>
What I want to do is to pass the variable i as the parameter to the UserControl so that it displays the number inside a, say, bordered div. 我想要做的是将变量i作为参数传递给UserControl,以便它显示一个数字,例如,有边框的div。
How is this possible? 这怎么可能?
Thanks 谢谢
<% 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. RenderPartial方法有一个重载,允许您传入(子)模型。 To use it most effectively, your UserControl should be strongly typed - in this case to a model of type System.Int32.
要最有效地使用它,您的UserControl应该是强类型的 - 在本例中是System.Int32类型的模型。
To use it in a UserControl: 要在UserControl中使用它:
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<System.Int32>" %>
<div><%= this.Html.Encode(this.Model) %></div>
In this case, this.Model
is a System.Int32 instance. 在这种情况下,
this.Model
是一个System.Int32实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.