[RESOLVED]How to fetch the value from dropdownlist in MVC?

In my one of the view I add a dropdown and I bind this drop down with my database like this..

public ActionResult PostMarks()
        {
            JoinMod std = new JoinMod();
            std.Drp_bind = (from nm in db.TbStudent
                            select new SelectListItem
                            {
                                Text = nm.StudentName,
                                Value = SqlFunctions.StringConvert((double)nm.StudentId).Trim()
                            }).ToList<SelectListItem>();
       

            return View(std);

        }

Here is the dropdownlist in my view

<p>StudentName</p>
    @Html.DropDownList("StudentName",Model.Drp_bind,"Select")


And on Post I am trying to save the data into the database like this 

 [HttpPost]
        public ActionResult PostMarks(Marks marks)
        {
           
            db.TbMarks.Add(marks);
            db.SaveChanges();
            return RedirectToAction("ShowAllMarks");



        }

Now when I check my database after save data in database the Id save in the database is zero from the dropdownlist. Please experts help me to solve this issue

Azad Chouhan

@Html.DropDownList("StudentName",Model.Drp_bind,"Select")

since you name dropdown with StudentName, Does your Marks have the property StudentName? 

Azad Chouhan

PostMarks(Marks marks)

The marks is populated by MVC model binding. This binding happens by naming convention. Please take a look at this article to understand how this works.

Understanding ASP.NET MVC Model Binding

Leave a Reply