开发者

Help required with a radio button based MVC User Control

I have an MVC user control that displays radio buttons in my MVC app. The issue is that how do I get it to display unique q group name for each control. I need to somehow pass it a parameter to that the name is not set as the same as every other group of radio buttons that I have used the control for on the page.

If this were not MVC I would know how to do this straight away.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<td><input type="radio" name="need" value="4"/></td>
<td><input type="radio" name="need" value="3"/></td>
<td><input type="radio" name="need" value="2"/></td>
<td><input type="radio" name="need" value="1"/></t开发者_运维技巧d>

<td><input type="radio" name="current" value="4"/></td>
<td><input type="radio" name="current" value="3"/></td>
<td><input type="radio" name="current" value="2"/></td>
<td><input type="radio" name="current" value="1"/></td>

Thanks Andy


If you pass some model data to the user control, then you will be able to use this to group your radio buttons.

Change the top line to declare the model type you are passing, such as:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Group>" %>

Where the class 'Group' is a class of your making containing the group's name.

e.g.

public class Group
{
   public string Name { get; set; };
}

Then have your user control render the following:

<td><input type="radio" name="<%: Model.Name %>" value="4"/></td>
<td><input type="radio" name="<%: Model.Name %>" value="3"/></td>
<td><input type="radio" name="<%: Model.Name %>" value="2"/></td>
<td><input type="radio" name="<%: Model.Name %>" value="1"/></td>

You can then show this user control using the RenderPartial HTML helper from it's parent multiple times based on the group name that you require.

<% Html.RenderPartial("PartialControlName", new Group { Name = "need" }); %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜