【发布时间】:2018-08-29 19:02:47
【问题描述】:
我正在尝试为我的 BugTracker 实现多选下拉功能,但似乎找不到不涉及创建另一个模型的解决方案。以下是它的工作原理:
我有一个名为 Report 的模型,其中包含有关错误的信息。该模型包含诸如错误名称、紧急程度、提交日期、解决方案等字段。我想要的是添加一个实际上是相关错误列表的字段。因此,当程序员更新这个报告时,他可以从以前的错误(报告)列表中选择并添加多个报告编号。查看(未编辑)报告时,这些数字将显示为报告页面的 ActionLink。所以也许用户看到了这个:
相关错误:1、37、56
它背后的代码是这样的:
RelatedBugs: @Html.ActionLink("1", "Admin", "Report", 1, null), @Html.ActionLink("37", "Admin", "Report", 37, null), @ Html.ActionLink("56", "Admin", "Report", 56, null)
(这些数字实际上是下面的 Report.cs 的 ID。)
所以在表单的编辑视图中,会有一个多选下拉菜单,您可以在其中选择所有这些错误。这类似于https://harvesthq.github.io/chosen/,在多选区域中,您可以看到所有错误/报告及其相关 ID 的列表。甚至只是一个 ID 列表!
在典型的视图中,您会看到上面的内容。
我该怎么做才能让它正常工作?
让我给你我自己的代码来告诉你我的意思(代码被大大削减以使问题更容易):
Report.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(查看)
@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>
}
我的视图模型(真正的视图模型包含几个附加字段):
UserReportViewModel.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");
}
【问题讨论】:
-
感谢您询问如何使其正常工作,但我想知道读者是否需要更多关于您正在寻求的帮助的指导。例如,这是一个 UI 设计问题吗? SQL/数据库/模型/连接问题?你的代码目前做了什么,它与你想要的有什么不同?