2010-01-27 38 views
1

我正在尋找一種方法將參數傳遞給用戶控件?現在我的用戶控件中有一個很大的if語句和代碼重複。我想要在視圖中有循環,我想將循環的當前索引傳遞給用戶控件?將參數傳遞給ASP.NET MVC中的用戶控件?

// 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

您可以將任何模型傳遞給您的局部視圖。寬度這一行:

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

您通過頁面的局部視圖的電流模式(你不需要明確地做,因爲如果你不通過模型當前模型將被傳遞)。如果你想傳遞任何其他的東西,你只需要爲它創建一個類,從這個類中實例化一個對象,用你需要的任何數據填充它,然後把它傳遞給partialview。該類可以包含頁面的模型以及索引的簡單屬性。

相關問題