3

I am trying to bind a dropdown list from database in mvc3.

I have two tables. tblEmp: EmpID (pk), EName, Age, Address, EmailID, DeptID (fk).

tblDept DeptID (pk), DeptName, DeptHead.

I am trying to bind create an Employee application with the basic details of an employee Name, Age, Address, EmailID, and Dept Name. I am trying to bind the Dept Name dropdownlist from the other table.

This is my Model:

namespace MvcEmployeeApplication.Models
{

   public class UandPcompare
{
    public int EmpID { get; set; }
    public string EName { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public string EmailID { get; set; }
    public int DeptID { get; set; }
    public string DeptName { get; set; }
    public string DeptHead { get; set; }

    public IList<SelectListItem> Drp_DeptNames { get; set; }
}
}

This is Controller:

    [HttpGet]
    public ActionResult Create()
    {
        FillDeptName();        
        return View();
    }

    [HttpPost]
    public ActionResult Create(tblEmployee tblEmp)
    {
        test.Entry(tblEmp).State = System.Data.EntityState.Added;
        test.SaveChanges();
        return RedirectToAction("Index");

    }

    public ActionResult FillDeptName()
    {
        UandPcompare filldeptNme = new UandPcompare();            
        filldeptNme.Drp_DeptNames = (from DptName in test.tblDepts
                                     select new SelectListItem()
                                     {
                                         Text = DptName.DeptName,
                                         Value = SqlFunctions.StringConvert((double)DptName.DeptID)
                                     }).ToList<SelectListItem>();
        return View("Create");
    }

This is MyView:

@model MvcEmployeeApplication.Models.UandPcompare
@{
ViewBag.title = "Edit";
}
<h2> Create </h2>
@using (Html.BeginForm())
{
<fieldset>
<legend> Create </legend>

<div>
  Employee ID:  @Html.DisplayFor(model => model.EmpID)
</div>
<div>
  Employee Name:  @Html.EditorFor(model => model.EName)
</div>
<div>
  Email-ID:  @Html.EditorFor(model => model.EmailID)
</div>
<div>
  Address: @Html.EditorFor(model => model.Address)
</div>
<div>
  Dept Name: @Html.DropDownList("DeptName", Model.Drp_DeptNames, "Select")
</div>
<p>
<input type="submit" value="Create" />
</p>

<div>
@Html.ActionLink("Back to Index", "Index");
</div>
2
  • I think the best way to do this is write a javascript which would call an Action method which in turn will return a Json array of object. You can then bind these to the drop down using JQUERY. Commented Mar 6, 2014 at 9:59
  • Dont think its good idea, whats need of ajax ajax when he can pass SelectList from action only Commented Mar 6, 2014 at 10:01

1 Answer 1

2

Not able to get what error are you getting.

You are not passing any model to your view.

public ActionResult FillDeptName()
    {
        UandPcompare filldeptNme = new UandPcompare();            
        filldeptNme.Drp_DeptNames = (from DptName in test.tblDepts
                                     select new SelectListItem()
                                     {
                                         Text = DptName.DeptName,
                                         Value = SqlFunctions.StringConvert((double)DptName.DeptID)
                                     }).ToList<SelectListItem>();
        return View("Create",filldeptNme);//pass model to view here
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.