单击按钮时,在视图中添加列表框中的项目

我有这个模型

public class CreateAppointmentSelectPersons { [DataType(DataType.EmailAddress, ErrorMessageResourceType = typeof (Resource), ErrorMessageResourceName = "CreateAppointment_Email_Invalid_Email_Address")] [EmailAddress] [Display(ResourceType = typeof(Resource), Name = "RegisterViewModel_EmailId_Email_Id")] public string Email { get; set; } public Boolean IsSuperOfficeConnected { get; set; } [Display(ResourceType = typeof (Resource), Name = "CreateAppointmentSelectPersons_SelectedSuperOfficeEmail_SuperOffice_Contact")] public string SelectedSuperOfficeEmail { get; set; } public Boolean IsInternalAddressBookEmpty { get; set; } [Display(ResourceType = typeof(Resource), Name = "CreateAppointmentSelectPersons_InternalAddressBookPersons_AddressBook_Contact")] public string SelectedAddressBookPerson { get; set; } [Display(ResourceType = typeof (Resource), Name = "CreateAppointmentSelectPersons_AttendeesListBox_Invited_Persons")] [Required] public List AttendeesListBox { get;set; } } 

此外,我是视图中列表框的新手。 这个想法是用户最多有三个盒子,当他们输入一个电子邮件地址并点击它旁边的按钮时,我想将它添加到视图中的列表框中,我想仅在发布表单时从列表框中检索值。

这就是我目前所看到的,

 @using System.Web.Mvc.Html @model DatePicker.Models.ViewModels.Appointment.CreateAppointmentSelectPersons @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml";  } 

Create

@using(Html.BeginForm("Create","Appointment", new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.LabelFor(m => m.Email, new { @class = "col-md-8 control-label" })
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) >" />
@if (Model.IsSuperOfficeConnected) { @Html.LabelFor(m=>m.SelectedSuperOfficeEmail, new {@class="col-md-8 control-label"})
@Html.TextBoxFor(m=>m.SelectedSuperOfficeEmail,new{@class="form-control",PlaceHolder="Search in SuperOffice..."}) >">
} @if (Model.IsInternalAddressBookEmpty) { @Html.LabelFor(m => m.SelectedAddressBookPerson, new { @class = "col-md-8 control-label" })
@Html.TextBoxFor(m=>m.SelectedAddressBookPerson,new{@class="form-control",PlaceHolder="Search in AddressBook..."}) >">
} @Html.ListBoxFor(m => m.AttendeesListBox, new SelectList(Model.AttendeesListBox), new { style = "width:100%;" })
} @section Scripts{ @Scripts.Render("~/Scripts/jquery-ui-1.10.4.min.js") $(function() { $("#SelectedSuperOfficeEmail"). autocomplete({ source: '/Appointment/SuperOfficePerson', minLength: 1, }); }); $(function() { $("#SelectedAddressBookPerson").autocomplete({ source: '/Appointment/AddressBookPerson', minLength: 1, }); }); $(function() { $('#btnEmail').click(function(e) { var name = $('#Email').val(); $('#AttendeesListBox').append($("").attr("value"), name).text(name); }); }); $(function () { $('#btnSuperOffice').click(function (e) { var name = $('#SelectedSuperOfficeEmail').val(); $('#AttendeesListBox').append($("").attr("value"), name).text(name); }); }); $(function () { $('#btnEmail').click(function (e) { var name = $('#SelectedAddressBookPerson').val(); $('#AttendeesListBox').append($("").attr("value"), name).text(name); }); }); }

现在我得到一个抱怨说“无法解析ListBoxFor(lambda表达式)”。

我应该怎么做,让我的视图工作,以便在按钮单击时将值添加到列表框中,并在提交时我只获取列表框的值

这是解决方案 –

让你的模特 –

 public class ListBoxModel { public List Names { get; set; } public List SelectedNames { get; set; } } 

让你的控制器行动 –

 public class ListBoxController : Controller { public ActionResult Index() { ListBoxModel model = new ListBoxModel(); model.Names = new List(); model.Names.Add("Rami"); return View(model); } public ActionResult Submit(ListBoxModel model) { return null; } } 

索引控制器只是创建项目列表并将其发送到索引视图。 索引视图将是 –

 @model MVC.Controllers.ListBoxModel @{ ViewBag.Title = "Index"; } 

View1

@using (Html.BeginForm("Submit", "ListBox", FormMethod.Post)) { @Html.ListBoxFor(m => m.SelectedNames, new SelectList(Model.Names)) }

在索引视图中,当您在文本框中输入名称并单击“单击”按钮时,它将被添加到客户端上的列表框中 –

在此处输入图像描述

当您在ListBox中选择项目并单击“提交”按钮时,表单将以选定的值提交给Listbox Controller的“提交操作”。

在此处输入图像描述

UPDATE

根据问题更新,要将所有ListBox项发送到服务器,我们使用JQuery动态地在表单中添加隐藏字段。

查看 –

 @model MVC.Controllers.ListBoxModel @{ ViewBag.Title = "Index"; } 

View1

@using (Html.BeginForm("Submit", "ListBox", FormMethod.Post)) {
for (int i = 0; i < Model.Names.Count; i++) { @Html.Hidden("UserValues", Model.Names[i]); } @Html.ListBoxFor(m => m.SelectedNames, new SelectList(Model.Names)) }

并在控制器 –

  public ActionResult Submit(ListBoxModel model, IEnumerable UserValues) { var c = Request.Form; return null; } 

输出 – 所有选定的值都将出现在ListBoxModel中,但所有列表框项都将出现在UserValues参数中。

在此处输入图像描述