DropdownList With Data Entity Framework

Go To StackoverFlow.com

1

This is my DropdownList :

                        <div class="editor-label">
    <p>
        <%: Html.LabelFor(model => model.Gov) %>
    </p>
    </div>
    <div class="editor-field">
    <p>
        <%=Html.DropDownListFor(model => model.Gov, ViewBag.gov as SelectList)%>
        <%: Html.ValidationMessageFor(model => model.Gov) %>
    </p>

and this is my view controller :

        public ActionResult TestR()
    {
        ViewBag.gov = new SelectList(entity.gouvernerat, "Idgov", "Nomg");
        return View();
    }

With HttpPost :

 [HttpPost]
    public ActionResult TestR(LogModel.preRegister Model)
    {
        if (ModelState.IsValid)
        {
            if (LogModel.preRegister.Verifuser(Model.IDag))
            {
                ModelState.AddModelError("", "Agence existe deja");
                return View(Model);
            }
            else
            {
                int ins = LogModel.preRegister.Register(Model);
                if (ins > 0)
                {

                    return View("Index");
                }
                else return View();
            }
        }
        else
        {
            return View();
        }

    }

Now Dropdown list show me the list of Gov in my DB(that's what i want), but when i clic on create i got this error in my DropdownLisrt There is no ViewData item of type 'IEnumerable <SelectListItem>' with key 'Gov'.

2012-04-04 23:30
by Chlebta
http://stackoverflow.com/questions/1297399/populating-asp-net-mvc-dropdownlis - AliRıza Adıyahşi 2012-04-04 23:42


2

This is pretty easy with LINQ to SQL. Let's say you have a table Govs that contains your "Tunis", "Ariana", etc. strings. You can then create your select list like this:

govs.Select( x => new SelectListItem { Text = x.Name, Value = x.Name } );

Of course you can even be more flexible and assign the value to something else, like an ID.

Update: more details to answer questions posed in comments:

Add the select list items in the view model:

public IEnumerable<SelectListItem> GovItems { get; set; }

Then, in the controller, before returning your view:

// database code to get "Govs" table goes here....

var vm = new MyViewModel {
  GovItems = govs.Select( x => new SelectListItem { Text = x.Name, Value = x.Name } );
}

Then in your view:

@Html.DropDownListFor( x => x.Gov, Model.Govs )

I hope this helps. If you're still confused, I recommend reading some tutorials about ASP.NET MVC. I recommend Microsoft's official one:

http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part1-cs

2012-04-05 00:19
by Ethan Brown
Whare i put it in ModelClass Or Controlle - Chlebta 2012-04-05 00:32
I usually set things up in the controller, but you could make an argument for putting it in a view model. It certainly doesn't belong in a model since it's presentation-specific - Ethan Brown 2012-04-05 00:36
How to put it in Controler ? i can't do it sorry but i'm beginner in ASP.net MVC this is my first projec - Chlebta 2012-04-05 00:46
any helps please? - Chlebta 2012-04-11 21:11
Chlebta, please read the tutorial I referenced at the bottom of my answer, I think you need it. You just put it in the controller by...putting it in the controller. I'm not really sure what you're asking - Ethan Brown 2012-04-11 21:14
I have update my post can you check it - Chlebta 2012-04-11 22:25
You should have two actions in your controller: one for the HttpGet, one for HttpPost. Could you update the answer with the HttpPost action - Ethan Brown 2012-04-11 22:48
I have update it, and and i dont have HttpGe - Chlebta 2012-04-11 22:53
Thanks, looking now. HttpGet is the default option if you don't specify it - Ethan Brown 2012-04-11 22:58
You're not setting ViewBag.gov in your HttpPost action. So when you return View(), it isn't available, which is why you're getting that error. Set ViewBag.gov in your HttpPost action, and that should clear that error - Ethan Brown 2012-04-11 23:00
thank's it works now : - Chlebta 2012-04-11 23:12
Ads