[RESOLVED]read data from database into dropdownlist using mvc 5

I want to create a drop down list with the values of the table categories which has ID and category name into a drop down list using mvc 5.

This is my model for creating the table categories:

namespace DemoIdentity.Models
{
public class CategoriesAdmin
{
    public int ID { get; set; }
    [Required(AllowEmptyStrings = false)]
    [Display(Name = "category name")]
    public string categoryName { get; set; }
}
public class DefaultConnection:DbContext
{
   public DbSet<CategoriesAdmin> categories { get; set; }
}
}

add this in controller call LoadCountries() function in view action

 public void LoadCountries()
        {

            List<SelectListItem> li = new List<SelectListItem>();

            li.Add(new SelectListItem { Text = "Select", Value = "0" });
            li.Add(new SelectListItem { Text = "India", Value = "1" });
           ViewData["country"] = li;
        }


      public JsonResult GetStates(string id)
        {
            List<SelectListItem> states = new List<SelectListItem>();
            switch (id)
            {
                case "1":

                    states.Add(new SelectListItem { Text = "Select", Value = "0" });
                    states.Add(new SelectListItem { Text = "ANDAMAN & NIKOBAR ISLANDS", Value = "1" });
                    states.Add(new SelectListItem { Text = "ANDHRA PRADESH", Value = "2" });
                    states.Add(new SelectListItem { Text = "ARUNACHAL PRADESH", Value = "3" });
                    states.Add(new SelectListItem { Text = "ASSAM", Value = "4" });
                    states.Add(new SelectListItem { Text = "BIHAR", Value = "5" });
                    states.Add(new SelectListItem { Text = "CHANDIGARH", Value = "6" });
                    states.Add(new SelectListItem { Text = "CHHATTISGARH", Value = "7" });
                    states.Add(new SelectListItem { Text = "DADRA & NAGAR HAVELI", Value = "8" });
                                  

                    break;
              
            }

            return Json(new SelectList(states, "Value", "Text"));

        }

Add this in view

<script src="…/Scripts/jquery-1.10.2.js" type="text/javascript"></script>

<script type="text/javascript">// ‘ + state.Text + ”); }); }, error: function (ex) { alert(‘Failed to retrieve states.’ + ex); } }); return false; }) // ]]></script>

  @Html.DropDownList("Countries", ViewData["country"] as List<SelectListItem>)
    @Html.DropDownList("State", new SelectList(string.Empty, "Value", "Text"), "Please select a State", new { style = "width:250px", @class = "dropdown1" })

I don’t want to use json and as I mentioned earlier I want to read from database from a table category the field categoryName

Hi,

Please refer to the document

#Bind Dropdownlist Using View Model 

http://www.dotnetbull.com/2013/08/bind-dropdownlist-in-dotnet-mvc-razor.html

View:

@using (Html.BeginForm()) { @Html.DropDownListFor(x => x.istateid, Model.getState() , "--Choose Your State--")
modele:

public SelectList getState()

 {

  IEnumerable<SelectListItem> stateList = (from m in _db.mstrstates where m.bstatus == true select m).AsEnumerable().Select(m => new SelectListItem() { Text = m.vstate, Value = m.istateid.ToString() });

  return new SelectList(stateList, "Value", "Text", istateid);

 }

Hope it is helpful to you.

Leave a Reply