Asp.net mvc 基于self为multiselect下拉列表创建字段(不创建新模型)

Asp.net mvc 基于self为multiselect下拉列表创建字段(不创建新模型),asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我正在尝试为我的BugTracker实现一个多选下拉功能,但似乎找不到一个不涉及创建另一个模型的解决方案。下面是它的工作原理: 我有一个名为Report的模型,其中包含有关bug的信息。这个模型包含诸如bug名称、紧迫性、提交日期、解决方案等字段。我想要添加的是一个实际上是相关bug列表的字段。因此,当程序员更新此报告时,他可以从以前的bug(报告)列表中选择并添加多个报告编号。查看(未编辑)报告时,这些数字将显示为指向报告页面的操作链接。因此,用户可能会看到: 相关错误:1、37、56 它背后

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

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

相关错误:1、37、56

它背后的代码是这样的:

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

(其中这些数字实际上是下面Report.cs的ID。)

因此,在表单的“编辑”视图中,会出现一个多选下拉列表,您可以在其中选择所有这些错误。这类似于,在multiselect区域中,您可以看到所有Bug/报告及其关联ID的列表。甚至只是一个ID列表

在典型视图中,您可以看到上面的内容

我该怎么做才能让它正常工作

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

Report.cs(模型)

AdminReport.cshtml(视图)

控制器

(适用于AdminReport.cshtml)

对于AdminReportForm.cshtml

最后是我的帖子:

    [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;}
    }
}
namespace BugTracker.ViewModels
{
公共类UserReportViewModel
{
公共报表{get;set;}
公共列表报告{get;set;}
}
}

我终于解决了这个问题-谢谢大家的帮助!我是这样解决的:

首先,在我的“Report.cs”文件中,我添加了以下内容:

public String RelatedBugs {get; set;}
public String[] RelatedBugsArray {get; set;}
public IEnumerable<Report> Report {get; set;}
@Html.ListBoxFor(m => m.Report.RelatedBugsArray, new MultiSelectList(Model.Reports, "ID", "Name"), new {id = "relatedbugs"})
在我的视图模型“UserReportViewModel.cs”中,我添加了以下内容:

public String RelatedBugs {get; set;}
public String[] RelatedBugsArray {get; set;}
public IEnumerable<Report> Report {get; set;}
@Html.ListBoxFor(m => m.Report.RelatedBugsArray, new MultiSelectList(Model.Reports, "ID", "Name"), new {id = "relatedbugs"})
要使用所选脚本,我首先使用以下链接进行设置:

在来自的同一份报告中,我使用我选择的脚本制作了我的ListBoxFor:

@section scripts{
<script>
$(function () {
$("#relatedbugs").chosen();
});
</script>
}
对于SaveAdmin操作,我们只需要执行以下操作:

if (viewModel.Report.RelatedBugsArray == null)
     viewModel.Report.RelatedBugs = null;
else
     viewModel.Report.RelatedBugs = string.Join(",", viewModel.Report.RelatedBugsArray);
基本上,我们所做的是将报表ID作为一个数组,并将它们放入一个字符串中,每个元素之间用逗号分隔。当我们将其反馈给视图时,通过在逗号之间拆分每个元素,将字符串转换为数组


再次感谢所有的建议

我很感激你问我如何让它正常工作,但我想知道读者是否需要更多关于你正在寻求的帮助的指导。例如,这是一个UI设计问题吗?SQL/数据库/模型/联接问题?你的代码目前做什么,它与你想要的有什么不同?我真的很喜欢这个想法!这对于用户可以选择多个报告的表单视图来说是完美的。但是,我的报表模型将使用什么来保存用户选择的值呢。根据建议,我使用了建议:public List Reports{get;set;}为了实际保存数据,我在我的报告模型中添加了以下内容:public Virtual ICollection Reports{get;set;}也就是说,我还在表单上苦苦挣扎。。我正在尝试这样做:
@Html.DropDownListFor(m=>m.Reports,newmultiselectlist(Model.Reports,“ID”,“Name”),“Select Priority”,new{ID=“relatedbugs”,multiple=“multiple”})
,但是,我不太确定如何使用它访问我的列表。执行这行代码的正确方法是什么?“但是,我的报表模型使用什么来保存用户选择的值?”您可以根据需要向视图模型添加任意多的对象。创建另一个列表,列出所选报告。然后在post控制器中,您可以将该对象传递到您的上下文中并更新数据库。为什么不使用Tag Helper而不是@html.DropDownList呢?
@Html.ListBoxFor(m => m.Report.RelatedBugsArray, new MultiSelectList(Model.Reports, "ID", "Name"), new {id = "relatedbugs"})
@section scripts{
<script>
$(function () {
$("#relatedbugs").chosen();
});
</script>
}
if (report.RelatedBugs != null)
     report.RelgatedBugsArray = report.RelatedBugs.Split(',');
if (viewModel.Report.RelatedBugsArray == null)
     viewModel.Report.RelatedBugs = null;
else
     viewModel.Report.RelatedBugs = string.Join(",", viewModel.Report.RelatedBugsArray);