根据自身为多选下拉列表创建字段(不创建新模型)



我正在尝试为我的 BugTracker 实现多选下拉功能,但似乎找不到不涉及创建另一个模型的解决方案。以下是它的工作原理:

我有一个名为 Report 的模型,其中包含有关错误的信息。此模型包含错误名称、紧急程度、提交日期、解决方案等字段。我想要的是添加一个字段,该字段实际上是相关错误的列表。因此,当程序员更新此报告时,他可以从以前的错误(报告(列表中进行选择并添加多个报告编号。查看(未编辑(报表时,这些数字将显示为报表页面的操作链接。所以也许用户会看到这个:

相关错误: 1, 37, 56

它背后的代码是这样的:

相关错误: @Html.ActionLink("1", "Admin", "Report", 1, null(, @Html.ActionLink("37", "Admin", "Report", 37, null(, @Html.ActionLink("56", "Admin","Report", 56, null(

(这些数字实际上是报告的 ID.cs从下面开始。

因此,在表单的"编辑视图"上,将有一个多选下拉列表,您可以在其中选择所有这些错误。这类似于 https://harvesthq.github.io/chosen/,在多选区域中,您可以看到所有错误/报告及其相关 ID 的列表。甚至只是一个ID列表!

在典型的视图中,你会看到你上面有什么。

我该怎么做才能使其正常工作?

让我给你我自己的代码来告诉你我的意思(代码大幅削减以使问题更容易(:

报表.cs(模型(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace BugTracker.Models
{
public class Report
{
//For User
public int ID { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public String Solution { get; set; }    
}
}

AdminReport.cshtml (View(

@model BugTracker.Models.Report
@{
ViewBag.Title = "Report";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>@Model.Name</h1>
Description: @Model.Description <br /> <br />
Solution: @Model.Solution    <br /> <br>

以及用于编辑的表单...

AdminReportForm.cshtml

@model  BugTracker.ViewModels.UserReportViewModel
@{
ViewBag.Title = "Issue Form";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Update Issue</h1>    
@Model.Report.Name <br />    
@using (Html.BeginForm("SaveAdmin", "Report"))
{
<div class="form-group">
<label>Solution</label>
@Html.TextAreaFor(m => m.Report.Solution, new { @class = "form-control", @rows="10"})
</div>
@Html.HiddenFor(m => m.Report.ID)
<button type="submit" class="btn btn-primary">Save</button>
}

我的视图模型(真正的视图模型包含几个附加字段(:

用户报表视图模型.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BugTracker.Models;
using System.Web.Mvc;
namespace BugTracker.ViewModels
{
public class UserReportViewModel
{
public Report Report { get; set; }    
}
}

控制器。。。

(对于 AdminReport.cshtml(

public ActionResult Admin(int id)
{
var report = _context.Reports.SingleOrDefault(c => c.ID == id);
if (report == null)
return HttpNotFound();
var viewModel = new UserReportViewModel
{
Report = report,
};
return View("AdminReport", report);
}

对于 AdminReportForm.cshtml

public ActionResult EditAdmin(int id)
{
var report = _context.Reports.SingleOrDefault(c => c.ID == id);
if (report == null)
return HttpNotFound();
var viewModel = new UserReportViewModel
{
Report = report,
};
return View("AdminReportForm", viewModel);
}

最后是我的帖子:

[HttpPost]
[ValidateInput(false)]
public ActionResult SaveAdmin(UserReportViewModel viewModel)
{
if (viewModel.Report.ID == 0)
_context.Reports.Add(viewModel.Report);
else
var reportInDb = _context.Reports.Single(c => c.ID == viewModel.Report.ID);
_context.SaveChanges();
return RedirectToAction("ReportAll", "Report");
}

是否可以将报表集合添加到视图模型? 然后,可以使用 linq 筛选报表并填充下拉列表。

namespace BugTracker.ViewModels
{
public class UserReportViewModel
{
public Report Report { get; set; }
public List<Report> Reports {get; set;}
}
}

我终于能够解决这个问题 - 感谢大家的所有帮助!这是我解决它的方法:

首先,在我的">报告.cs"文件中,我添加了以下内容:

public String RelatedBugs {get; set;}
public String[] RelatedBugsArray {get; set;}

在我的视图模型">UserReportViewModel.cs"中,我添加了以下内容:

public IEnumerable<Report> Report {get; set;}

表格给了我一些问题。我最初使用的是 DropDownBoxFor,但对于多选,您实际上需要使用 ListBoxFor(它仍然可以与 DropDownBoxFor 一起使用,但问题是当您尝试编辑表单时它无法预填充它。所以,在AdminReportForm.cshtml中,我有以下内容:

@Html.ListBoxFor(m => m.Report.RelatedBugsArray, new MultiSelectList(Model.Reports, "ID", "Name"), new {id = "relatedbugs"})

要使用所选脚本,我首先使用此链接进行设置:在 MVC Asp Net 中使用选定的 jQuery 插件

在同一份报告中,我让我的ListBoxFor使用我的选择脚本:

@section scripts{
<script>
$(function () {
$("#relatedbugs").chosen();
});
</script>
}

现在,剩下的就是控制器了。我关心两个操作:编辑和保存操作。EditAdmin 和 Admin 操作将具有相同的代码:

if (report.RelatedBugs != null)
report.RelgatedBugsArray = report.RelatedBugs.Split(',');

对于 SaveAdmin 操作,我们只需要执行以下操作:

if (viewModel.Report.RelatedBugsArray == null)
viewModel.Report.RelatedBugs = null;
else
viewModel.Report.RelatedBugs = string.Join(",", viewModel.Report.RelatedBugsArray);

基本上,我们正在做的是将报告 ID 作为一个数组,并将它们放入一个字符串中,每个元素之间都有一个逗号。当我们将其反馈回视图时,通过在逗号之间拆分每个元素来将字符串转换为数组。

再次感谢所有建议!

最新更新