将ListBox的选定值传递给Controller

时间:2017-12-03 15:27:09

标签: c# asp.net-mvc forms

传真与员工enter image description here

之间存在多对多的关系

我想要一个传真表格,其中有一个列表框可供选择员工,但我不知道如何选择所选员工

FaxForm.cshtml:

@using (Html.BeginForm("CreateFax", "Fax"))
{
  @Html.AntiForgeryToken()

<div class="form-horizontal">


    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Fax.Courier_Num, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Fax.Courier_Num, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Fax.Courier_Num)
        </div>
    </div>

    <div class="form-group">

        <div class="col-md-10">
            @Html.ListBox("Employees", ViewBag.Employees as MultiSelectList,
            new { @class = "chzn-select", data_placeholder = "Choose Employee..." })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

FaxController.cs:

public ActionResult New()
    {
        var Employees = Db.Employees;
        ViewBag.Employees = new MultiSelectList(Employees, "Id", "Name");
        return View("FaxForm");

    }
    public ActionResult CreateFax(Fax fax)
    {

        if (!ModelState.IsValid)
        {
         //some Code
            return View("FaxForm"/*, viewModel*/);
        }


        if (fax.Id == 0)
        {
            Db.Faxes.Add(fax);
            Db.SaveChanges();
        }
        return RedirectToAction("Index", "Employees");
    }

我创建了viewmodel类来建立员工和传真之间的关系 MenuViewModel.cs:

public IEnumerable<SelectListItem> Employees { set; get; }
    public Fax Fax { set; get; }

我需要将选定的员工保存在传真表中..........................

1 个答案:

答案 0 :(得分:2)

您应该使用特定于视图的视图模型。不要将您的实体模型与之混合。

public class SendFaxVm
{
    public List<SelectListItem> Employees { set; get; }
    public int[] SelectedEmployees { set; get; }

    public string CompanyName { set; get; }
    public string CompanyAddress { set; get; }
    // To do : Add other properties needed in the VIEW
}

现在在您的GET操作中,创建一个此对象,加载Employees属性并将其发送到视图

public ActionResult New()
{
   var vm= new SendFaxVm();
   vm.Employees = db.Employees
                    .Select(a => new SelectListItem() {Value = a.Id.ToString(),
                                                       Text = a.Name})
                    .ToList();
   return View(vm);
}

现在在您的视图中,强烈键入我们的SendFaxVm,使用辅助方法生成文本框和多选下拉列表

@model SendFaxVm
@using (Html.BeginForm("CreateFax", "Fax"))
{
    @Html.TextBoxFor(a => a.CompanyName)
    @Html.TextBoxFor(a => a.CompanyAddress)
    @Html.ListBoxFor(a => a.SelectedEmployees, Model.Employees)
    <input type="submit" />
}

并使用与HttpPost操作方法的参数相同的视图模型。提交表单时,将从表单发送的数据填充属性。 SelectedEmployees属性将是已选择的UserId数组。您可以读取这些属性值并将其保存到实体表中。

[HttpPost]
public ActionResult CreateFax(SendFaxVm model)
{
   // check model.SelectedEmployees and other properties
   //  and use that to save data to your tables
   Fax f=new Fax();
   f.CompanyName = model.CompanyName;
   f.CompanyAddress = model.CompanyAddress;
   // to do : Assign other property values for the Fax table
   db.Fax.Add(f); 
   db.SaveChanges();

   //Now loop through the SelectedEmployees and save record for FaxData table
   foreach(var userId in model.SelectedEmployees)
   {
       var fd=new FaxData { EmpId = userId, FaxId=f.Id };
       //to do  : Save fd 
   }

   return RedirectToAction("Index");
}