Asp.net mvc 4 绑定mvc4中下拉列表中的数据集合
在我看来,我有下面这样的代码Asp.net mvc 4 绑定mvc4中下拉列表中的数据集合,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,在我看来,我有下面这样的代码 @if (!Model.DisableBuyButton || !Model.DisableWishlistButton) { <div class="qutblk"> <span>@Html.LabelFor(model => model.EnteredQuantity, new { @class = "input-small" }, " :")</span> @if (Model.Al
@if (!Model.DisableBuyButton || !Model.DisableWishlistButton)
{
<div class="qutblk">
<span>@Html.LabelFor(model => model.EnteredQuantity, new { @class = "input-small" }, " :")</span>
@if (Model.AllowedQuantities.Count > 0)
{
@Html.DropDownListFor(model => model.EnteredQuantity, Model.AllowedQuantities, new { @class = "input-small" })
}
else
{
@Html.TextBoxFor(model => model.EnteredQuantity, new { @class = "input-small" })
}
} @Html.Widget("productdetails_add_info")
</div>
@if(!Model.DisableBuyButton | | |!Model.DisableWishlistButton)
{
@LabelFor(model=>model.EnteredQuantity,新的{@class=“input small”},“:”)
@如果(Model.allowedQuantilities.Count>0)
{
@Html.DropDownListFor(model=>model.EnteredQuantity,model.allowedQuantilities,new{@class=“input small”})
}
其他的
{
@Html.TextBoxFor(model=>model.EnteredQuantity,新的{@class=“input small”})
}
}@Html.Widget(“产品详细信息添加信息”)
这里,如果我有允许的数量。COUNT
大于0。我需要数量的下拉列表
例如,如果数量为5,我需要下拉列表中的1,2,3,4,5
数字。如果数量为7,我需要下拉列表中的1,2,3,4,5,6,7
数字
但我的问题是,我无法绑定下拉列表中的数字之类的数据,这意味着如果数量为5,它只会在下拉列表中显示5个数字,而不显示1,2,3,4,5
您可以尝试以下方法:
public static class MyLists
{
public static List<SelectListItem> GetList()
{
List<SelectListItem> result = new List<SelectListItem>(int AllowedQuantities)
for(int i = 0; i < AllowedQuantities; i++)
{
var number = i + 1;
var item = new SelectListItem {text = number.ToString(), value = number.ToString()};
result.Add(item);
}
return result
}
}
您的视图没有任何逻辑来生成像1,2,3,4,5
这样的数字,如果您给它5作为数量。你怎么能指望razor做到这一点?你为什么不写一个自定义的助手来做你想做的事情呢?我已经写了below@Html.DropDownListFor(model=>model.EnteredQuantity,model.GetList(model.EnteredQuantity),new{@class=“input small”})显示错误,如“model name不存在当前上下文”
@Html.DropDownListFor(model => model.EnteredQuantity, MyLists.GetList(model.EnteredQuantity), new { @class = "input-small" })