You must Sign In to post a response.
  • Category: ASP.NET

    Bind Database to Dropdownlist Without "EF" in Mvc 4

    How To Load the database to Dropdownlist in Mvc 4.

    MY Controller

    [HttpPost]
    public ActionResult Index(FormCollection Fmn)
    {
    Register rg = new Register();
    rg.Name = Fmn["Name"];
    if (rg.InsertName(rg) == rg.Name)
    {
    ViewBag.Status = "Data Have Been Saved !";
    }
    }

    Model

    public String InsertName(Register Rmt)
    {
    string strSql = "Select UserName from [userpf]";
    SqlCommand strCmd = new SqlCommand(strSql,con);
    con.Open();
    int i = strCmd.ExecuteNonQuery();
    con.Close();
    return Name;
    }
    return View(rg.Name);
    }
    Now how can we populate the record in dropdownlist using mv c 4
  • #763983
    The code which you posted is not clear.

    Following are some common tips. You can handle it according to your requirement.

    Way 1

    1. Design your display model clearly.
    2. In your Display model have one property of type "List".
    3. Fill the list from the database using ADO.net(If you do not have EF)
    4. So that you can bind the list in your view.

    Way 2

    1. Create one action for getting list of data for drop down
    2. Using the JQuery ajax call, call that action.
    3. Fill the drop down from the ajax call result

    Way 3

    1. Simple send the list of data using ViewData or ViewBag
    2. Bind the value from ViewData to your Drop down box.

    By Nathan
    Direction is important than speed

  • #764005
    Hai V.Narayanaprasad,
    You just need to create a model which will have the same structure you want to fill in your drop-down list. after that you need to fill this model with the database and get the list. This list can be binded to the drop-down.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments