开发者

Passing parameters to ASCX user control in MVC

开发者 https://www.devze.com 2023-01-08 21:35 出处:网络
I am trying to pass a string parameter to an ASCX. I want to set this to the text property of a label. In the code below it shows betwen the div tags (ignore the % signs in the html tags).

I am trying to pass a string parameter to an ASCX. I want to set this to the text property of a label. In the code below it shows betwen the div tags (ignore the % signs in the html tags).

<@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" >  
<%asp:Label ID="Label2" runat="server" **Text="<%# Model %> Te开发者_运维问答st"** CssClass="isslabel"><%/asp:Label>  
<%div><%: Model %><%/div>   

However in the label tag no matter what I put between the angle brackets (bit it bold) in the text tag I cannot get the parameter to appear. I have tried <%: Model %> to no avail. Is the issue that the code block is inside quotes and am I just missing some character?


Why are you trying to use a web forms user control with MVC?

Assuming the model is the string you want to display and you are passing this through correctly, along the lines of

<% Html.RenderPartial("MyUserControlView", "My String To Display"); %>

In your "parent" page, you will be able to do the following in your ascx:

<%= Html.Label(Model) %>

Instead of <asp:label...

Update

If you need to specify then you have a number of options, you could wrap the Html.Label call in a div and specify the class of the div (updating your css accordingly), you could use a display template, or simply explicitly use the Html like the following:

<label for="someIdThatICouldUseAnotherHtmlExtensionMethodToGet"><%: Model %></label>

The key problem with your code (as now also pointed out in the comments by @mare) is that you are trying to use a web forms control in an MVC view.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号