简体   繁体   中英

MVC 4 DropDownList can't add to model

Hi I am new to MVC 4 and have run into an error when trying to use Html.DropDownList.

Here is my current code.

Model for Jobs:

  namespace Payroll.CORE
  {
  public class Job
  {
     public int JobID { get; set; }
     public string JobTitle { get; set; }

    public virtual ICollection<Employee> Employees { get; set; }
  }
}

Model for Employee:

    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public int PhoneNumber { get; set; }
    public int CellNumber { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public int PostalCode { get; set; }
    public string BankName { get; set; }
    public int BankAccountNumber { get; set; }
    public string PayInterval { get; set; }
    public double NormalRate { get; set; }
    public double OvertimeRate { get; set; }
    public double SetHours { get; set; }
    public System.DateTime DateOfEmployment { get; set; }
    public System.DateTime LastPaymentRecieved { get; set; }
    public string Active { get; set; }
    public int JobID { get; set; }

    public virtual ICollection<Absent> Absents { get; set; }
    public virtual ICollection<Bonus> Bonuses { get; set; }
    public virtual Job Job { get; set; }
    public virtual ICollection<Holiday> Holidays { get; set; }
    public virtual ICollection<HoursWorked> HoursWorkeds { get; set; }
    public virtual ICollection<Payrolls> Payrolls { get; set; }
    public virtual ICollection<Deduction> Deductions { get; set; }

View:

     <div class="editor-label">
        @Html.LabelFor(model => model.JobID)
    </div>
    <div class="editor-field">
        @Html.DropDownList("ListSL")

    </div>

Controller:

   [HttpGet]
    public ActionResult Create()
    {

        List<SelectListItem> jobs = new List<SelectListItem>();
        List<Job> list = bll.GetJobCatergories();

        foreach (var a in list)
        {
            jobs.Add(new SelectListItem { Text = a.JobTitle, Value = a.JobID.ToString()       });
        }

        ViewBag.ListSL = jobs;
        return View();
    }

I now have no way of getting the value of JobID into my model. I have tried using @Html.DropDownListFor but cant get it to populate with the SelectListItem.

I really have no problem getting it to work with hiddenfields or anything so any help would be greatly appreciated.

Thanks in Advance.

Edit: Sorry should have been a bit more clear. I am creating an employee and the employee gets assigned a job.So I am trying to add the selected Items list value to the Value int JobID in Employee.

Also the values are showing in the dropdownlist, I need to add the selected value to the employee model when submitting.

As i cant see your model...

@Html.DropDownListFor( x => x.JobIdSelected, new SelectList(Model.JobId, "Value", Model.JobId) )

In your model create a property, JobIdSelected and it will bind.

Note you will loose your Original Select list, so if you have any model errors, you will need to resend the list back to the view.

Okay, now the model's there, try:

@Html.DropDownListFor(mdl => mdl.Job.JobID,
                      new SelectList(ViewBag.ListSL,
                          "Value",
                          "Text",
                          Model.JobID),
                      "-- Please Select --")

I'd also add that, personally, I don't like the use of ViewBag, as you lose the static typing and hence need that cast back to the SelectList in this case. Whenever I need a drop down list, I have a static property on the model that returns all the items that will populate the drop down list, so I end up with, for example:

new SelectList(Model.AllJobs,....)

It should be

@Html.DropDownListFor(m => m.JobID, (IEnumerable<SelectListItem>)ViewBag.ListSL)

This binds the selected value to the property JobID, using ViewBag.ListSL as the options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM