2

In asp.net Mvc3 razor, I have binded some data with dbcontext in my controller viewbag using selectlist

My controller..

public ActionResult Index()
        {
            ViewBag.students = new SelectList(db.StudentList, "StudentID", "StudentName");
            return View();
        } 

Then I binded it to the ListBox using viewbag

My View..

@using (Html.BeginForm("Save", "Student"))
{    
    @Html.ValidationSummary(true)
    <div>
     @Html.ListBox("students")
    <p>
        <input type="submit" name="Save" id="Save" value="Save" />
    </p>
    </div>
}

Now, in my controller, in that Save action, I need to capture the listbox selected values I have tried the following

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(FormCollection formValue) 
        {
            //need code to capture values                
            return View("Index");
        }

Could anyone help

Thanks in advance

2 Answers 2

3

Try the following

 @Html.ListBox("students",ViewBag.students )

From form collection get the value of 'students'. For that please refer the following page

Pulling ListBox selected items from FormCollection

For a nice implementation of listbox in MVC please read this article.

ASP.NET MVC Select List Example

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

3 Comments

here I need to bind data to the listbox from database
Please read the last article. In that article see how data is filled in GetOptions() method. Instead of filling the hardcode value enumerate through table rows and fill data.
In your code in the following line replace the code as ViewBag.students = new SelectList(getStudents(), "StudentID", "StudentName"); Then make a method as getStudents which return studentID and studentName values from DB.
0

Try that:

public class StudentController : Controller
{
    //
    // GET: /Student/

    public ActionResult Index()
    {
        var studentList = new List<Student>
                              {
                                  new Student {StudentID = 1, StudentName = "StudentName1"},
                                  new Student {StudentID = 2, StudentName = "StudentName2"},
                                  new Student {StudentID = 3, StudentName = "StudentName3"},
                                  new Student {StudentID = 4, StudentName = "StudentName4"}
                              };

        ViewBag.students = new SelectList(studentList, "StudentID", "StudentName");
        return View();
    } 

    [HttpPost]
    public ActionResult Save(String[] students)
    {
        return View();
    }

}

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
}

2 Comments

here I need to bind data from database
It is sample data, behavior the data will be definitely same, i don't want to post very huge db code to that question.

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.