如何将数据从视图传递到ASP.NET MVC中的UserControl? [英] How to pass data from view to UserControl in ASP.NET MVC?

查看:204
本文介绍了如何将数据从视图传递到ASP.NET MVC中的UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想要做的最简单的传递数据如下:

 <%对于i = 0到10 %GT; 
<%Html.RenderPartial(的MyUserControl)%>
<%下一步%GT;



我想要做的是我通过该变量作为参数传递给用户控件,以便它显示一,也就是说,镶上DIV中的号码。



这怎么可能?



感谢


解决方案

  <%对于i = 0到10%GT; 
<%Html.RenderPartial(的MyUserControl我)%GT;
<%下一步%GT;



的RenderPartial方法有一个重载,允许您在一个(子)模型通过。使用它最有效,您的用户控件应强类型 - 在这种情况下,以类型System.Int32的模型






要在用户控件使用它:

 <%@控制语言=C#
继承=系统.Web.Mvc.ViewUserControl&所述; System.Int32>中%GT;
< DIV><%= this.Html.Encode(this.Model)%GT;< / DIV>

在这种情况下, this.Model 是一个System.Int32实例。


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

解决方案

<% 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.

这篇关于如何将数据从视图传递到ASP.NET MVC中的UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆