想要创建类似的票务系统。 票务创建者如何添加新评论有困难。
在下面的当前代码中,我在集合中添加新记录并将其显示为可编辑。但是,它不会绑定整个模型,只会隐藏idCR。
我想到CRCase模型中的其他成员处理新评论。 public CRParticipation NewCRParticipantion {get;set;}
但我收到了无效列错误,因为它不存在于表中。
非常感谢任何指导。
型号:
public class CRCase
{
[Key]
public int idCR { get; set; }
public string CRNo { get; set; }
public DateTime SubmittedDate { get; set; }
public string Responsible { get; set; }
public virtual ICollection<CRParticipation> CRParticipations { get; set; }
}
public class CRParticipation
{
[Key]
public int IdCRParticipation { get; set; }
public string CRStatus { get; set; }
public DateTime UpdatedDate { get; set; }
public string Comments { get; set; }
public int CR { get; set; }
[ForeignKey("CR")]
public CRCase CRCase { get; set; }
}
控制器:
public ActionResult Details(int id = 0)
{
CRCase crcase = db.CRCases.Find(id);
if (crcase == null)
{
return HttpNotFound();
}
else
{
//adding new record for new comments. might not be the proper approach.
crcase.CRParticipations.Add(new CRParticipation());
}
return View(crcase);
}
[HttpPost]
public ActionResult NewComment(CRCase crcase)
{
if (ModelState.IsValid)
{
//code here
return RedirectToAction("Details", new { id = crcase.idCR});
}
return View(crcase);
}
显示和添加新评论查看:
@model CRManagement1.Models.CRCase
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
@using (Html.BeginForm("NewComment", "CRCase")) {
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.idCR)
<fieldset>
<legend>CRCase</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.CRReferenceNo)
</div>
<!-- more fields here t display -->
<div class="display-field">
<table>
<tr>
<th>CRStatus</th>
<th>UpdatedDate</th>
<th>Comments</th>
</tr>
@foreach (var item in Model.CRParticipations)
{
<tr>
@if( item.IdCRParticipation != 0 )
{
<td>
@Html.DisplayFor(modelItem => item.CRStatus)
</td>
<td>
@Html.DisplayFor(modelItem => item.UpdatedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments)
</td>
}
else
{
<td colspan="3" class="editor-field">
@Html.EditorFor(model => model.CRParticipations.Last().Comments)
@Html.ValidationMessageFor(model => model.CRParticipations.Last().Comments)
</td>
}
</tr>
}
</table>
</div>
</fieldset>
<p>
<input type="submit" value="Post" />
</p>
}
<p>
@Html.ActionLink("Back to List", "Index")
</p>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
查看输出。如果您注意到输入了新注释,但单击“发布”按钮时它将不会在模型中绑定。