如何在MVC中防止@ Html.DropDownListFor中的相同值的多个选择

时间:2019-03-22 11:56:11

标签: jquery asp.net asp.net-mvc dropdown html.dropdownlistfor

我希望限制用户在同一下拉列表中选择已经选择的值。我的屏幕看起来像这样:

enter image description here

在每个“添加”上,单击“服务类型”和“服务位置”将被添加到部分视图中。对于此下拉菜单,我使用@Html.DropdownListFor来绑定数据库中的值。

<div class="row mt-3" id="servicelocation">
    @using (Html.BeginCollectionItem("ServiceTypeCollection"))
    {
    <div class="col-lg-3 col-sm-6 col-12">
        <label> Service Type</label>
        @Html.DropDownListFor(x => Model.Id, null, new { @class = "md-form droplist service-type" })
        @Html.HiddenFor(x => Model.Id)
    </div>

}

在[HttpGet]中

 public ActionResult Register(string guid)
    {
          var db = new VendorManagementContext();
          var types = db.ServiceTypes.Select(x =>
                     new SelectListItem()
                     {
                         Text = x.Name.ToString(),
                         Value = x.Id.ToString()
                     }).ToList();

            model.serviceTypes=types;
    }

谁能建议我如何限制其选择的值,使其不会在下一个下拉列表中显示?

已编辑

我们正在这样调用部分方法

[HttpGet]
        public ActionResult GetPartial()
        {
            try
            {
                var model = new ServiceTypePartial();
                ViewBag.Id = Utility.ServiceTypes();
                ViewBag.ZipCodeId = Utility.ZipCodes();
                return PartialView("_ServiceTypePartialView", model);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

并查看其

<div id="AddServices">
    @Html.CollectionEditorFor(m => m.ServiceTypeCollection, "Editors/_ServiceTypePartialView", "/Account/GetPartial",
       "Add service location", new { @class = "btn btn-default" })
 </div>

0 个答案:

没有答案