简体   繁体   中英

How to save my dropdown list selected Item into my database?

I need to save a selected Item in my Database during User Registration. but it seems as if my selected Item is not recognised. here is the error that it's givin me

"There is no ViewData item of type 'IEnumerable' that has the key 'Faculties"

Am still unskilled in MVC/C# Programming please help here is my code below; thanks in advance!

My DataModel

public string Faculty { get; set; }

My Controller

public ActionResult Register()
{
   DataClasses1DataContext db = new DataClasses1DataContext();
   ViewBag.Faculties = new SelectList(db.Faculties, "Id", "Name");
   return View();
}

My View

<div class="form-group">
    @Html.LabelFor(model => model.Faculty, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("Faculties","Select Faculty")
        @Html.ValidationMessageFor(model => model.Faculty, "", new { @class = "text-danger" })
    </div>
</div>

Try changing this line:

ViewBag.Faculties = new SelectList(db.Faculties, "Id", "Name");

to the following

ViewData["Faculties"] = new SelectList(db.Faculties, "Id", "Name");

ViewBag and ViewData are two separate constructs, and cannot be used interchangeably.

It's just the names of your property and your ViewBag are different. change your ViewBag name to match the property name.

ViewBag.Faculty = new SelectList(db.Faculties, "Id", "Name");

Your HTML would be:

 @Html.DropDownList("Faculty ","Select Faculty")

Alternatively and (preferably) use a model binding instead of ViewBag

Model

public string Faculty { get; set; }
public IList<SelectListItem> Faculties {get;set;}

Controller

Model.Faculties = new SelectList(db.Faculties, "Id", "Name");
return View(Model);

HTML (View)

@Html.DropDownListFor(m => m.Faculty , Model.Faculties )

The way you are displaying items in the dropdown is not correct. You can use below code to display the items fetched from your db:

    @Html.DropDownList("Faculties", ViewBag.Faculties as IEnumerable<SelectListItem>, 
                       "Select Faculty");

Please note that your ViewBag.Faculties should be casted to Enumerable<SelectListItem> .

To get the selected value of dropdown in controller you can use below method:

var value = Request["Faculties"];

Once you got the value, you can save it in database.

Update:

A good approach will be to bind your View to a model which I think you have already done since I can see you are using model.Faculty . So the dropdown should look something like below in View:

@Html.DropDownList(model => model.Faculty,ViewBag.Faculties as IEnumerable<SelectListItem>,
                  "Select Faculty");

And your controller where data is posted should be something like below:

[HttpPost]
public ActionResult Register(YourModel model)
{
    var selectedFaculty = model.Faculty; //Selected Value
    //Save it in database
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM