1
votes

I am having Code like below in my view.

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

Here if i have the ALLOWEDQUANTITIES.COUNT is more then 0.I need drop down list for quantities.

For example if the quantity is 5 i need 1,2,3,4,5 numbers in my drop down list.And if the quantity is 7 i need 1,2,3,4,5,6,7 numbers in my drop down.

But my problem is i am not able to bind the data like numbers in my drop down that means if the quantity is 5 it simply displaying only 5 number in drop down list not displaying 1,2,3,4,5.

1
Your view doesn't have any logic to generate numbers like 1,2,3,4,5 if you give it 5 as quantity. How can you expect razor to do it ? Why don't you write a custom helper to do as you expect ?Karthik Chintala

1 Answers

2
votes

You may try something like this:

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

In your view:

@Html.DropDownListFor(model => model.EnteredQuantity, MyLists.GetList(model.EnteredQuantity), new { @class = "input-small" })