ASP.NET Core:将模型绑定到多个选择列表时出现问题

我有一个正在处理相关数据的 Web 应用程序,我创建了一个推荐实体,其中包含其他属性之间的患者、请求者和此推荐的不同顾问。我还有患者、请求者和顾问模型。


我已经能够使用患者和请求者的选择列表,他们与转介实体具有 1:n 关系。但是,我与顾问之间存在问题,因为他们具有 n:m 关系,我正在使用多个列表,我可以显示我的顾问列表并选择多个顾问,但当我发布数据时,它不绑定到 ReferralConsultants我正在失去它。


这是引用控制器的 [get] 编辑操作。


var referral = await _context.Referral

                .Include(r => r.Patient)

                .Include(r => r.Requester)

                .Include(r => r.ReferralConsultants)

                    .ThenInclude(r => r.Consultant)

                .AsNoTracking()

                .FirstOrDefaultAsync(r => r.ID == id);


            if (referral == null)

            {

                return NotFound();

            }

            ViewData["Patients"] = new SelectList(_context.Patient, "ID", "FullName", referral.PatientID);

            ViewData["Requesters"] = new SelectList(_context.Requester, "ID", "FullName", referral.RequesterID);

            var consultants =  from c in _context.Consultant

                                    select new

                                    {

                                        ConsultantID = c.ID,

                                        c.FullName

                                    };


            ViewData["Consultants"] = new MultiSelectList(consultants, "ConsultantID", "FullName", referral.ReferralConsultants);

            return View(referral);


这是我的观点


<form asp-action="Edit">

            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <input type="hidden" asp-for="ID" />

            <div class="form-group">

                <h3>Patient Information</h3>

                <label asp-for="PatientID" class="control-label">Patient</label>

                <select asp-for="PatientID" class="form-control" asp-items="ViewBag.Patients"></select>

                <span asp-validation-for="PatientID" class="text-danger"></span>


我唯一缺少的是当我处于[帖子]编辑操作时能够知道哪些顾问已被选择。


江户川乱折腾
浏览 64回答 2
2回答

繁花不似锦

在模型中Referral,将 的类型更改ReferralConsultants为ICollection<int>(或任何类型Consultant.ID),对于选择元素,只有值(ConsultantID在本例中)包含在表单提交中。如果您在浏览器中检查视图的生成源,这一点就会变得很清楚。

慕的地6264312

我最终解决了这个问题,执行以下操作:我创建 MultiSelectList 项并将其发送到视图,如下所示。ReferralsController.csvar consultants =&nbsp; from c in _context.Consultant&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConsultantID = c.ID,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.FullName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewData["Consultants"] = new MultiSelectList(consultants, "ConsultantID", "FullName", referral.ReferralConsultants.Select(r => r.ConsultantID).ToArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return View(referral);在视图中&nbsp; &nbsp;<div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label d-inline">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Consultants&nbsp; &nbsp; &nbsp; &nbsp; <select multiple name="selectedValues" class="form-control" asp-items="ViewBag.Consultants"></select>&nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp; &nbsp;</div>当我提交表单时,这会将 selectedValues 发送到我的控制器。然后我只需要在编辑[帖子]中获取这些值并将它们保存在数据库中。public async Task<IActionResult> Edit(int id, [Bind("ID,PatientID,RequesterID,DateIssued,DateRequested,Description,Type, Status")] Referral referral, string [] selectedValues)
打开App,查看更多内容
随时随地看视频慕课网APP