使用MVC4我想实现允许用户向数据库添加新项目的功能。
我设法将这些项添加到单个表中,但现在我需要显示多个表中的数据,然后将添加/选择的数据填充到这些表中。
我有这3张桌子
威胁
ThreatHasSecurityEvent
SecrutiyEvents
到目前为止,这是我的代码:
ViewModel
public class ThreatWithSecurityEvents
{
public Threat Threat { get; set; }
public SecurityEvent SecurityEvent { get; set; }
public List<int> SecurityEventIds { get; set; }
public ThreatWithSecurityEvents()
{
SecurityEventIds = new List<int>();
}
}
获取控制器
[HttpGet]
public ActionResult AddNewThreat()
{
ThreatWithSecurityEvents ViewModel = new ThreatWithSecurityEvents();
var SecurityEvents = _DBContext.SecurityEvents.Select(x => new SelectListItem()
{
Text = x.Description,
Value = x.ID.ToString()
});
ViewBag.SecurityEventDropdown = SecurityEvents;
return View(ViewModel);
}
查看
@model RiskAssesmentApplication.Models.ThreatWithSecurityEvents
@{
ViewBag.Title = "AddNewThreat";
//Layout = "~/Views/Shared/MasterLayout.cshtml";
}
<div style="font-family: Calibri">
<h2>AddNewThreat</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Threat</legend>
@using (Html.BeginForm("Add New Threat", "Threats"))
{
Html.HiddenFor(model => model.SecurityEventIds);
<div class="editor-label">
@Html.LabelFor(model => @Model.Threat.Description, "Threat Description")
</div>
<div class="editor-field">
@Html.EditorFor(model => @Model.Threat.Description)
@Html.ValidationMessageFor(model => @Model.Threat.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => @Model.SecurityEvent.Description, "Associated Security Event")
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.SecurityEventIds, ViewBag.SecurityEventDropdown as IEnumerable<SelectListItem>)
</div>
<p>
<input type="submit" value="Add New" />
</p>
}
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
我不确定如何在存储库中实现Post Action Method和Save Method。 以前我可以注入一个新的威胁对象并将其发送到编辑视图,执行以下操作:
上一个获取方法 - AddNewThreat
[HttpGet]
public ActionResult AddNewThreat()
{
return View("EditThreat", new Threat());
}
然后我会使用EditThreat Action Method回发
上一篇文章操作 - AddNewThreat
[HttpPost]
public ActionResult EditThreat(Threat Threat)
{
if (ModelState.IsValid)
{
repository.SaveThreat(Threat);
TempData["message"] = string.Format("{0} new description has been saved", Threat.Description);
return RedirectToAction("GetThreat", new { ThreatID = Threat.ID });
}
else
{
// something is incorrect!
return View(Threat);
}
}
以前保存方法 - 从存储库保存文件
public void SaveThreat(Threat Threat)
{
if (Threat.ID == 0)
{
_context.Threats.Add(Threat);
}
else
{
Threat dbEntry = _context.Threats.Find(Threat.ID);
if (dbEntry != null)
{
dbEntry.Description = Threat.Description;
}
}
_context.SaveChanges();
}
到目前为止,这是我的目标。
我希望用户能够输入新的威胁描述,然后从下拉列表中选择一个安全事件或多个事件,这些事件将与新威胁相关联。
我意识到我将不得不更改控制器中的post back操作方法和我的存储库中的Save方法,但我无法弄清楚如何将新的威胁描述和现有的安全事件保存回数据库。我已经进行过搜索,但截至尚未发现/了解任何内容。
任何建议/帮助都会很棒。
谢谢
答案 0 :(得分:1)
我认为实现这一目标的最简单方法是&#34;划分&#34;你的表格分成几步。
您有2个实体:威胁,SecurityEventID
威胁有一系列SecurityEvents
使用自定义ViewModels而不是原始类将数据发送到控制器。例子:
public class AddThreatViewModel
{
public string Description { get; set; }
//since it's a add view model, we dont need a ThreatId here
}
[HttpPost]
public ActionResult AddThreat(AddThreatViewModel model)
{
//convert the view model to Threat, add to database
}
public class AddThreatEvent
{
public int ThreatId { get; set; }
public int SecrutiyEventId { get; set; }
}
[HttpPost]
public ActionResult AddThreatEvent(AddThreatEventmodel)
{
//add threat event into existing threat
}
答案 1 :(得分:1)
您应该查看模型
public class NewThreatVM
{
public string Description { get; set; } // add validation attributes as required
public List<int> SelectedSecurityEvents { get; set; }
public SelectList SecurityEventList { get; set; } // or IEnumerable<SelectListItem>
}
附注:创建视图中不需要Threat.ID
属性,但是如果您想要使用它来编辑现有的Threat
,请添加属性int? ID
并使用{ {1}}在POST方法中确定它是新的还是现有的if (model.ID.HasValue)
和简化视图
Threat
附注:您的视图不应包含安全事件ID的隐藏输入(您无法将输入绑定到复杂对象或集合)
然后控制器
@model yourAssembly.NewThreatVM
@Html.BeginForm())
{
@Html.TextBoxFor(m => m.Description)
@Html.ListBoxFor(m => m.SelectedSecurityEvents, Model.SecurityEventList)
<input type="Submit" value="Create" />
}