将参数传递给ASP.NET MVC中的用户控件?

时间:2010-01-27 14:56:45

标签: asp.net-mvc user-controls

我正在寻找一种将参数传递给用户控件的方法?现在我的用户控件中有一个很大的if语句和代码重复。我想在视图中有循环,我想将循环的当前索引传递给用户控件?

// current view
Html.RenderPartial("DetailsRateForm", ViewData.Model);

// ASP.NET MVC User control

  <% else
 {%>
 <%=Html.Hidden(Resources.RSINET.RateDetailIndex, "0")%>

<table class="prettyForm">
<thead>
    <th colspan="2">Add Rate Details</th>
</thead>
<tr>
    <td>Effective Date</td>
    <td><%=Html.TextBox(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceEffectiveDate)%>  <a href="javascript:NewCal('RateDetail[<%="0"%>].EffectiveDate','mmddyyyy')"><img src="../../Content/Images/cal.gif" width="16" height="16" border="0" alt="Pick a date"/></a></td>
</tr>
<tr>
    <td>Expiration Date</td>
    <td><%=Html.TextBox(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceExpirationDate)%> <a href="javascript:NewCal('RateDetail[<%="0"%>].ExpirationDate','mmddyyyy')"><img src="../../Content/Images/cal.gif" width="16" height="16" border="0" alt="Pick a date"/></a></td>
</tr>
<tr>
    <td>Condition Type</td>
    <td><%=Html.DropDownList(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceConditionType, ViewData.Model.CondT, "Choose Option")%></td>
</tr>
<tr>
    <td>Condition Value</td><td><%=Html.TextBox(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceConditionValue)%></td>
</tr>
<tr>
    <td>Rate</td><td><%=Html.TextBox(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceRate)%> </td>
</tr>
<tr>
    <td>Unit</td><td><%=Html.DropDownList(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceUnit, ViewData.Model.Unit, "Choose Option")%></td>
</tr>
<tr>
    <td>Status</td>
    <td><%=Html.DropDownList(Resources.RSINET.RateDetailBrace + "0" + Resources.RSINET.BraceActiveItem, ViewData.Model.Active, "Choose Option")%></td>
</tr>

</table>

<%} %>

1 个答案:

答案 0 :(得分:1)

您可以将任何模型传递到局部视图。宽度此行:

Html.RenderPartial("DetailsRateForm", ViewData.Model);

您将页面的当前模型传递给局部视图(您不需要显式执行此操作,因为如果您未传递模型,将传递当前模型)。如果你想传递任何其他内容,你只需为它创建一个类,从该类中实例化一个对象,用你需要的任何数据填充它并将其传递给partialview。该类可以包含页面的模型以及带索引的简单属性。