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

六月ゝ 毕业季﹏ 提交于 2019-12-10 21:45:20

问题


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.

How is this possible?

Thanks


回答1:


<% 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 should 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, this.Model is a System.Int32 instance.




回答2:


  • you do not. This stuff must be in the model, suppsedly.
  • PLease upgrade to MVC v2 ;) get rid of the strings.


来源:https://stackoverflow.com/questions/2507729/how-to-pass-data-from-view-to-usercontrol-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!