代码之家  ›  专栏  ›  技术社区  ›  ryan

MVC2中的简单下拉列表?

  •  2
  • ryan  · 技术社区  · 14 年前

    我有一个将真/假值存储为sqlserver位字段的表( Featured

    第二,这是我到目前为止得到的有用但没有补充的东西 selected="selected" 到DDL中的任何项。

    编辑1: 编辑2: 像函数一样使用它可以工作(有或没有强制转换),但是selected=“selected”仍然没有被添加。

    //
    // View models
    public class SpotlightFormViewModel
    {
    
        // props
        public Spotlight Spotlight { get; private set; }
        public SelectList FeaturedMenu { get; private set; }
    
        static IDictionary<string, int> feature = new Dictionary<string, int>(){
            {"True", 1},
            {"False", 0},
        };
    
        public IEnumerable<SelectListItem> FeaturedChoices(Spotlight spotlight)
        {
            return feature.Select(f => new SelectListItem
            {
                Text = f.Key,
                Value = f.Value.ToString(),
                Selected = spotlight.Featured,
            });
        }
    
        // constr
        public SpotlightFormViewModel(Spotlight spotlight)
        {
            Spotlight = spotlight;
            FeaturedMenu = new SelectList(FeaturedChoices(spotlight));
        }
    }
    

    控制器:

        public ActionResult Edit(int id)
        {
            Spotlight spotlight = spotlightRepository.GetSpotlight(id);
    
            return View(new SpotlightFormViewModel(spotlight));
        }
    

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Spotlight.Featured) %>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.FeaturedMenu, Model.FeaturedChoices(Model.Spotlight))%>
                <%: Html.ValidationMessageFor(model => model.Spotlight.Featured) %>
            </div>
    
    2 回复  |  直到 11 年前
        1
  •  3
  •   Ted Gueniche    14 年前

    我和你一样使用dropdowlist。但在你的情况下,你可以简单地使用一些单选按钮:

     <%: Html.Label("True") %>
     <%: Html.RadioButtonFor(model => model.FeaturedMenu, "1") %>
    
     <%: Html.Label("False") %>
     <%: Html.RadioButtonFor(model => model.FeaturedMenu, "0") %>
    

    return feature.Select(f => new SelectListItem
    {
        Text = f.Key,
        Value = f.Value.ToString(),
        Selected = false
    });
    
        2
  •  1
  •   ryan    14 年前

    我终于成功了。即使我的语法;当我把变量传递给 public IEnumerable FeaturedChoices(Spotlight spotlight) 不管怎样,即使我明确地标记 Selected = true . 如果有人能回答为什么会这样我会很感激

    唯一的办法 selected="selected" 如果我做了 FeaturedChoices

    视图模型:

    //
    // View models
    public class SpotlightFormViewModel
    {
    
        // props
        public Spotlight Spotlight { get; private set; }
        public SelectList FeaturedMenu { get; private set; }
    
        static IDictionary<string, int> feature = new Dictionary<string, int>(){
            {"True", 1},
            {"False", 0},
        };
    
        public IEnumerable<SelectListItem> FeaturedChoices
        {
            get
            {                
                return feature.Select(f => new SelectListItem
                {
                    Text = f.Key,
                    Value = f.Value.ToString(),
                    Selected = IsSelected(this.Spotlight.Featured, f.Key)
                });
            }
        }
    
        private bool IsSelected(bool featured, string val)
        {
            bool result = false;
    
            if (String.Compare(val, "True") == 0 && featured)
                result = true;
            else if (String.Compare(val, "False") == 0 && !featured)
                result = true;
    
            return result;
        }
    
        // constr
        public SpotlightFormViewModel(Spotlight spotlight)
        {
            Spotlight = spotlight;
        }
    }
    

    控制器:

        public ActionResult Edit(int id)
        {
            Spotlight spotlight = spotlightRepository.GetSpotlight(id);
    
            return View(new SpotlightFormViewModel(spotlight));
        }
    

    查看:

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Spotlight.Featured) %>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.FeaturedMenu, Model.FeaturedChoices)%>
                <%: Html.ValidationMessageFor(model => model.Spotlight.Featured) %>
            </div>