Tuesday 26 September 2017

Get Selected Value from DropDownList in ASP.Net MVC Razor.

Get Selected Value from DropDownList in ASP.Net MVC Razor
Description:


In this example we explain that how to Get Selected value from Dropdown List in Asp.Net Razor View. Or how to get the selected text or value of the dropdown list in MVC razor view. Or how to get or set the selected value of the dropdown list in MVC 4.
Model:

public class EmployeeModel
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }

Controller: 

public class EmployeeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(EmployeeModel Employee)
        {
            int EmployeeId = Employee.EmployeeId;
            string name = Employee.Name;
            string gender = Employee.Gender;
            string city = Employee.City;

            return View();
        }
    }

View: 

@model Form_Post_MVC.Models.EmployeeModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Get Selected Value from DropDownList in ASP.Net MVC Razor</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Employee Details</th>
            </tr>
            <tr>
                <td>EmployeeId: </td>
                <td>
                    @Html.TextBoxFor(m => m.EmployeeId)
                </td>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td>
                    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
                   { new SelectListItem{Text="Male", Value="M"},
                     new SelectListItem{Text="Female", Value="F"}}, "Please select")
                </td>
            </tr>
            <tr>
                <td>City: </td>
                <td>
                    @Html.TextBoxFor(m => m.City)
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    }
</body>
</html>



This entry was posted in :

0 comments:

Post a Comment