MVC4根据选择列表中的项目数查看决策

时间:2014-01-14 02:58:12

标签: c# asp.net-mvc asp.net-mvc-4 razor

我正在使用Razor View引擎创建一个MVC4应用程序,我想根据View中的项目数在我的Select List中做出决定。我的选择列表在控制器中创建并保存在ViewData集合中。基本上,如果选择列表中只有一个项目可用,我打算隐藏视图中的下拉列表并使用隐藏字段对值进行硬编码。

当我访问“Items”属性时,Intellisense不提供计算列表项数量的方法。如何计算Razor View中选择列表中的项目?

Index.cshtml

@model Mvc4.Models.PhoneRecord
...
    <div class="editor-field">
        @{
            // I need logic here to count items in the select list.
            // XXXXX is of course a compile error, there is no Intellisense
            // option that provides a "count" method, what should I use?
            if (((SelectList)ViewData["_CellPhoneCarrier"]).Items.XXXXX == 1)) 
            {
                @Html.Raw("note: value is hardcoded.")
                @Html.HiddenFor(m => m.CellPhoneCarrier)
            }
            else
            {
                @Html.DropDownListFor(m => m.CellPhoneCarrier, (SelectList)ViewData["_CellPhoneCarrier"])
            }
        }
    </div>
...

3 个答案:

答案 0 :(得分:2)

要直接回答您的问题,您只需要使用Count()方法,就像这样:

if (((SelectList)ViewData["_CellPhoneCarrier"]).Items.Count() == 1))

但是,我同意阿列克谢。使用强类型模型时,创建视图会更容易。你可以这样:

public class CellPhoneCarriersViewModel
{
    public SelectList Items { get; set; }
    public int CellPhoneCarrier { get; set; }
}

现在,您可以在控制器中创建SelectList而不是将ViewData存储在public ActionResult Index() { var model = new CellPhoneCarriersViewModel(); model.Items = new SelectList(...); return View(model); } 内,而是直接访问它:

@if (Model.Items.Count() == 1)
{
    @Html.HiddenFor(m => m.CellPhoneCarrier)
}
else
{
    @Html.DropDownListFor(m => m.CellPhoneCarrier, Model.Items)
}

不再需要在视图中乱扔东西了。因此视图变为:

public static class HtmlExtensions
{
    public static HtmlString HiddenOrDropDownFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
        Expression<Func<TModel, TProperty>> target, IEnumerable<SelectListItem> selectList)
    {
        if (selectList.Count() == 1)
            return helper.HiddenFor(target);

        return helper.DropDownListFor(target, selectList);
    }
}

如果您打算在多个地方使用此功能,可以通过编写扩展方法为您执行此操作,从而进一步清理您的视图:

@Html.HiddenOrDropDownFor(m => m.CellPhoneCarrier, Model.Items)

然后在你的视图中使用它:

{{1}}

答案 1 :(得分:1)

您应该将SelectionList放在模型中。然后在您的视图中,您可以执行以下操作:

      @{
        int nbrOfCarrier = Model.CellPhoneCarrierList().Count();

        if (nbrOfCarrier > 1)
        {
            @Html.DropDownListFor(m => m.CellPhoneCarrier,Model.CellPhoneCarrierList())   
        }
        else
        {
            @Html.HiddenFor(m => m.CellPhoneCarrier, "1")
        }

    }

并在模型中,

   public List<SelectListItem> CellPhoneCarrierList()
    {
        List<SelectListItem> x = new List<SelectListItem>();
        x.Add(new SelectListItem { Text = "AT&T", Value = "1" });
        x.Add(new SelectListItem { Text = "T-Mobile", Value = "2"});
        x.Add(new SelectListItem { Text = "Verizon", Value = "3" });
        return x;
    }

使用Entity Framework和Stored Procedures填充选择列表。甚至为DAL和BLL添加其他层以实现可伸缩性。除了基于用户输入在模型中使用不同方法处理的语句和决策之外,我永远不会将代码放在控制器中

答案 2 :(得分:0)

在viewmodel中创建手机运营商的集合,然后在此视图中使用该viewmodel。

又名:

public class PhoneRecordVM
{
    public ICollection<CellPhoneCarrier> CellPhoneCarriers { get; set; }

    public PhoneRecordVM(Customer customer)
    {
        CellPhoneCarriers = (db.CellPhoneCarrier)
            .Where(e => e.CarrierZone == customer.Zone)
            .OrderBy(e => e.CellPhoneCarrier.Text))
            .ToList();
    }
}

在视图中:

if(SelectList(Model.CellPhoneCarriers, "ID", "CellPhoneCarrier.Text", Model.SelectedCellPhoneCarrierID).Count ==1)
{...}
else
{...}
相关问题