Hi friends, in this article I will explain about How to create a DropDownList field for MVC4 Razor view?
I already explained in the previous articles about MVC 4 Razor: Send Email with multiple attachments in ASP.NET using C#, How to bind Dropdownlist inside WebGrid in MVC 4 Razor or how to populate dropdownlist inside webgrid in ASP.NET with MVC 4 Razor using C# and Create/Generat PDF with image Using RazorPDF in ASP.Net MVC
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