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

时间:2021-07-09 16:48:15

I am looking for a way to pass an parameter to the user control? Right now I have a big if statement and code duplication in my user control. I want to have the loop inside the view and I want to pass the current index of the loop to the user control?

我正在寻找一种方法将参数传递给用户控件?现在我的用户控件中有一个很大的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 个解决方案

#1


1  

You can pass any model to your partial view. Width this line:

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

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

You pass the current model of the page to the partial view (you do not need to explicitly do that as the current model will be passed if you do not pass a model). If you want to pass anything else you just create a class for that, instantiate a object from that class, fill it with whatever data you need and pass it to the partialview. This class can contain the model of the page as well as a simple property with the index.

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

#1


1  

You can pass any model to your partial view. Width this line:

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

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

You pass the current model of the page to the partial view (you do not need to explicitly do that as the current model will be passed if you do not pass a model). If you want to pass anything else you just create a class for that, instantiate a object from that class, fill it with whatever data you need and pass it to the partialview. This class can contain the model of the page as well as a simple property with the index.

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