我有以下代码呈现了一个称为ExchangeRates.ascx的控件。我想将两个变量传递给控件,currencyCode和toCurrencyCode,但我只知道如何传递一个。有人可以帮忙吗?
<% var currencyCode = Html.Encode(Model.Country.CurrencyCode); %>
<% var toCurrencyCode = Html.Encode(Model.Country.toCurrencyCode); %>
<% Html.RenderPartial("~/Views/Shared/ExchangeRates.ascx", currencyCode); %>
最佳答案
您需要定义一个新模型,其中包含您感兴趣的两个属性:
public class CurrencyCodesViewModel
{
public string CurrencyCode { get; set; }
public string ToCurrencyCode { get; set; }
}
然后将您的部分视图强力键入此模型并传递其实例:
<% Html.RenderPartial(
"~/Views/Shared/ExchangeRates.ascx",
new CurrencyCodesViewModel {
CurrencyCode = currencyCode,
ToCurrencyCode = toCurrencyCode
}
); %>