0

想创建一个类似的票务系统。对工单创建者如何添加新评论有困难。

在下面的当前代码中,我在集合中添加新记录并将其显示为可编辑。但是,它并没有绑定整个模型,只绑定了隐藏的 idCR。

我想到了 CRCase 模型中的其他成员来处理新的评论。例如,public CRParticipation NewCRParticipantion {get;set;}但我收到了 Invalid Column 错误,因为它在表中不存在。

非常感谢任何指导。

模型:

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")
}

查看输出。如果你注意到新的评论被输入了,但是当点击“发布”按钮时它不会被绑定到模型中。 在此处输入图像描述

4

0 回答 0