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'.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Whare i put it in ModelClass Or Controller
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.
How to put it in Controler ? i can't do it sorry but i'm beginner in ASP.net MVC this is my first project
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.