html 仅显示选定值中的一个选定值的MultiSelectList

slsn1g29  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(132)

对于httppost方法,MultiSelectList在视图端只显示一个值。
我的看法

<div class="col-md-6 form-group">
   <select class="form-control selectpicker" required="" asp-for="DomID" asp-items=" ViewBag.DomList as MultiSelectList" multiple data-live-search="true"
                                                    placeholder="Select Categories"
                                                    onchange="console.log($(this).children(':selected').length)">
   </select>
</div>

MY控制器

[HttpPost] 
 public IActionResult controllerAction([Bind]LogModel logmodel)  
{ 
    logmodel.selectedvals=  // Has Submitted/selected values 
    List<LogModel> domList = new List<LogModel>();
    domList = ;//getting values for dropdown
    ViewBag.DomList = new MultiSelectList(domList , "DomID", "Dom", logmodel.selectedvals);
    return view(logmodel);
 }

我的下拉菜单仅显示从所有选定值中选择的一个值,即
日志模型。后操作中的选定值。
如果我在httpget方法中为multiselectlist设置硬编码选定值,那么它会正确地显示选定值,我在这里做错了什么?
更新

Log Model
 public Int64 DomID { get; set; }
 public string Dom { get; set; }
 public List<Int64> selectedvals { get; set; }
mftmpeh8

mftmpeh81#

尝试从return view(logmodel);中删除logmodel
下面是一个作品演示,大家可以参考一下,希望能有所帮助。
在控制器中:

public IActionResult SetList()
        {
            return View(); 
        }
        [HttpPost]
        public IActionResult ShowMList(LogModel logmodel)
        {
            List<LogModel> domList = new List<LogModel>();

            domList.Add(new LogModel() { DomID = 1, Dom = "AA" });
            domList.Add(new LogModel() { DomID = 2, Dom = "BB" });
            domList.Add(new LogModel() { DomID = 3, Dom = "CC" });
            ViewBag.DomList = new MultiSelectList(domList, "DomID", "Dom", logmodel.selectedvals);
            return View();
        }

SetList.cshtml:

@model LogModel
<form asp-action="ShowMList">
    <select name="selectedvals" class="form-control" multiple>
        <option value="1">AA</option>
        <option value="2">BB</option>
        <option value="3">CC</option>
    </select>
    <input type="submit" value="submit" />

</form>

ShowMList.cshtml:

@model LogModel

<div class="col-md-6 form-group">
    <select class="form-control selectpicker" required="" asp-for="DomID" asp-items=" ViewBag.DomList as MultiSelectList" multiple data-live-search="true"
            placeholder="Select Categories"
            onchange="console.log($(this).children(':selected').length)">
    </select>
</div>

结果:

qnakjoqk

qnakjoqk2#

在aspfor int中使用int而不是<int>

相关问题