Friday, 8 September 2017

Access DropDownList and DropDownListFor Value From View To Controller In Asp.Net MVC, C#.Net

9/08/2017 - By Pranav Singh 1

In this article I will show you how you can how you can access dropdownlist and dropdownlistfor value from view to controller in mvc, c#.net. I will show separate dropdownlist and dropdownlistfor selected value.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC_Demos.Models
{
    public class StudentModel
    {
        public List<Student> StudentList { get; set; }
        public int SelectedStudentId { get; set; }
    }
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
After this we will add a controller class file. In this we will create some list value for showing in dropdown list.
    public ActionResult Index()
        {
            StudentModel _studentmodel = new StudentModel();
            _studentmodel.StudentList = new List<Student>();
            _studentmodel.StudentList.Add(new Student { Id = 1, Name = "Student1" });
            _studentmodel.StudentList.Add(new Student { Id = 2, Name = "Student2" });
            _studentmodel.StudentList.Add(new Student { Id = 3, Name = "Student3" });
            return View(_studentmodel);
        }

Now first we will check for

DropDownListFor:

         ///


        /// Example for DropDownList
        /// 
        ///
        [HttpPost]
        public ActionResult Index(StudentModel _studentmodel)
        {
            _studentmodel.StudentList = new List<Student>();
            _studentmodel.StudentList.Add(new Student { Id = 1, Name = "Student1" });
            _studentmodel.StudentList.Add(new Student { Id = 2, Name = "Student2" });
            _studentmodel.StudentList.Add(new Student { Id = 3, Name = "Student3" });
            return View(_studentmodel);
        }

      
Here in above code I have passed the student model class as parameter. Now generate the view and add the below code.

@model MVC_Demos.Models.StudentModel
@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm("Index", "DropDownList"))
{
    <b>Example Of DropDownListFor </b><br />
    @Html.DropDownListFor(m=>m.SelectedStudentId, new SelectList(Model.StudentList, "Id", "Name"), "Select Name")<br />
    <input type="submit" value="Submit" />
}


In above code I have bind the dropdownlistfor with model. Now run it and check the output.

DropDownList:

For this add the below code.

         ///


        /// Example for DropDownList
       ///
        ///
        [HttpPost]
        public ActionResult Index(string SelectedStudentId)
        {
            StudentModel _studentmodel = new StudentModel();
            _studentmodel.StudentList = new List<Student>();
            _studentmodel.StudentList.Add(new Student { Id = 1, Name = "Student1" });
            _studentmodel.StudentList.Add(new Student { Id = 2, Name = "Student2" });
            _studentmodel.StudentList.Add(new Student { Id = 3, Name = "Student3" });
            return View(_studentmodel);
        }

        
Now we will create view and add the below code.

@model MVC_Demos.Models.StudentModel
@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm("Index", "DropDownList"))
{
  <b>Example Of DropDownList </b><br />
    @Html.DropDownList("SelectedStudentId", new SelectList(Model.StudentList, "Id", "Name"), "Select Name")<br />
    <input type="submit" value="Submit" />
}


Here in above code you can check the name of dropdownlist is same as the parameter passed into the index post method.

Now run it and check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top