开发者

set Dropdownlist in mvc 3.0

using (var db  = new PNUBOOKIR.Models.KowsarSiteEntities())
        {
            var q = (from m in db.GoodTypes
                    select new
                    {
                        GroupCode = m.GroupCode,
                        Text = m.Text
                    }).ToList();

        }         

How I can set it in dropdownlist GroupCode as Value and Text as ItemText How I can do that in mvc 3.0 ?

Added :

        public ActionResult Index()
    {
        using (var db = new PNUBOOKIR.Models.KowsarSiteEntities())
        {
            var q = from m in db.GoodTypes
                    where !m.GroupCode.Equals(null) && !m.Text.Equals(string.Empty) && !m.Text.Equals(null)
                    select new
                    {
                        GroupCode = m.GroupCode,
                        Text = m.Text
                    };
            ViewData["list"] = q.ToList().Select(t => new SelectListItem()
                             {
                                 Text = t.Text,
                                 Value = t.GroupCode.ToString(),
                                 Selected = true
                             });
        }      
        return View();
    } 

  <div>
        @using (Html.BeginForm())
        {

            System.Collections.IEnumerable ien = (System.Collections.IEnumerable)ViewData["list"];
    开发者_StackOverflow        Html.DropDownList("lstGoodTypes", new SelectList(ien, "Value", "Text"));

        }
    </div>

it works but there is no dropdownlist at the result there is no error and there is not dropdownlist it is cool.


In your controller you can do:

ViewData["list"]=q.Select(t=>new SelectListItem
                                 {
                                     Text=t.Text,
                                     Value=t.GroupCode,
                                     Selected=<Here you have to return a boolean to decide if the item is selected>
                                  });

And in your view you can add this to a form:

@Html.DropDownList("list")

In your form's post method you'll need to have a parameter named list of the same type as GroupCode and treat it accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜