簡體   English   中英

將列表框的選定值傳遞給控制器

[英]Pass Selected values of ListBox to Controller

我在傳真和員工之間有很多關系 在此處輸入圖片說明

我想有一個傳真列表,其中有一個用於選擇雇員的列表框,但我不知道如何獲得選定的雇員

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; }

我需要將選定的員工保存在傳真表中。

您應該使用特定於視圖的視圖模型。 請勿將您的實體模型與此混用。

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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM