ASP.NET MVC 4 Want to populate dropdown list from database

asp.net-mvc-4 entity-framework html-select linq

Question

I am new guy in ASP.NET MVC 4. I want to populate dropdownlist from database table BO where Column name is Id, Code, Name, OrgId. I want to bind two Code & Namecolumn's data to DataTextfield and Id column Data to DataValueField of dropdown. I have created code for this which are as follows BUT ITS NOT RETURNING DATA FROM TABLE and var BOList is remain empty :

my connectionstring is 


 <add name="iRegDBContext"
  connectionString="Data Source=****;Initial Catalog=iReg;User ID=**;Password=****;Integrated Security=True"
  providerName="System.Data.SqlClient"
/>

My Controller class :

public class iRegController : Controller
{
    private iRegDBContext l_oDbBO = new iRegDBContext();  

    // GET: /iReg/

    public ActionResult PopulatejQgrid()
    {
        var BOList = l_oDbBO
                     .BO
                     .ToList()
                     .Select(d => new SelectListItem
                         {
                             Value = d.Id.ToString(),
                             Text = d.Name + "[ " + d.Code + " ]"
                         });
        ViewBag.BOData = new SelectList(BOList, "Value", "Text"); 
        return View();
    }
}

My Model class :

public class BO
{
    public Guid Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

public class iRegDBContext : DbContext
{
    public DbSet<BO> BO { get; set; }
}

My cshtml class :

@model MvciReg.Models.BO

@{
    ViewBag.Title = "PopulatejQgrid";
}

@using (Html.BeginForm())
{
    <fieldset>
        BO : 

        @Html.DropDownList("BOData")
        <p>
            <input type="submit" value="Go" />
        </p>
    </fieldset>
}

I really don't know where I am going wrong. I developed my code from reference of following link Click here . Kindly suggest correction in code ...

UPDATE: I tried following Matt Bodily's code in my controller and what I see is code is not fetching data from database and that code is

    public ActionResult populatejQgrid()
    {
        ViewBag.BOData = GetDropDown();

        return View();
    }

    public static List<SelectListItem> GetDropDown()
    {
        List<SelectListItem> ls = new List<SelectListItem>();
        var lm = from m in db.BOs //fetch data from database
                 select m;
        foreach (var temp in lm)
        {
            ls.Add(new SelectListItem() { Text = temp.Name, Value = temp.Id.ToString() });
        }
        return ls;
    }

In Controller :

      @Html.DropDownList("BOData", (List<SelectListItem>)ViewBag.BOData)

But when I saw value of ls through watch it always show me Count = 0 but its not giving me any error.

I found something new this problem. When I kept mouse pointer over var lm; it shows me query and in query table name in FROM clause is not that one in my SQL database. My SQL table name is BO and in query it is taking BOes. I don't know from where this name is coming. I think this is the main cause of all this problem So How I overcome this??

1
3
10/14/2013 7:22:14 AM

Popular Answer

First Create a BO list for Dropdownlist in VIEW

@{
    var Bolst= Model.BO.Select(cl => new SelectListItem
       {
           Value = cl.Value.ToString(),
           Text = cl.Text== null ? String.Empty : cl.Text
       });
}

@(Html.DropDownList("sampleDropdown", BOlst, "-----Select-----"))

In Controller:

    return View(BOlst); // why use Viewbag when directly pass it to view
3
10/14/2013 6:52:17 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow