Showing posts with label DropDownList in HTML Helper in MVC. Show all posts
Showing posts with label DropDownList in HTML Helper in MVC. Show all posts

Saturday, 23 November 2013

DropDownList in HTML Helper in MVC


DropDownList in HTML Helper in MVC

It is little bit tricky to create dropdown list in MVC using HTML Helper class. As we know that in general dropdown list we show data in Text and Value format.

Similarly here we have to create a class which contains the property for Text and Value.

For Example:-

In this example I am creating dropdown list for countries. In which I am taking CID as a value field and CName as a Text Field.

Create controller name home and method index and create view for index method.

To add properties, add class and give it name country and declare two properties CID and CName.

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

namespace MvcApplication7
{
    public class country
    {
        public int CID { get; set; }
        public string CName { get; set; }
    }
}

Now go to your index view and create a list of type country and add Items in this list.