Sunday, 30 November 2014

How to create a DropDownList field for MVC4 Razor view?


Hi friends, in this article I will explain about How to create a DropDownList field for MVC4 Razor view?

Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcNew.Models;

namespace MvcNew.Controllers
{
    public class HomeController : Controller
    {
            public ActionResult DropDown()
        {
            List<SelectListItem> items = new List<SelectListItem>();

            items.Add(new SelectListItem { Text = "Developer", Value = "0" });

            items.Add(new SelectListItem { Text = "Analyst", Value = "1" });

            items.Add(new SelectListItem { Text = "Admin", Value = "2", Selected = true });

            items.Add(new SelectListItem { Text = "Manager", Value = "3" });

            ViewBag.Desig_Type = items;

            return View();      
        }
    }
}


View:
@model MvcNew.Models.Home

@{
    ViewBag.Title = "Drop";
}

<h2>Drop</h2>



   @using (Html.BeginForm("Category", "Home", FormMethod.Get)) {

    <fieldset>

            Designation

            @Html.DropDownList("Desig_Type")

        <p>

            <input type="submit" value="Submit" />

        </p>

    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



The output of the above code as shown in the below figure.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.