C# 如何在同一视图中的模型的ICollection成员中添加新记录

C# 如何在同一视图中的模型的ICollection成员中添加新记录,c#,asp.net-mvc-4,model,one-to-many,C#,Asp.net Mvc 4,Model,One To Many,希望创建一个类似的票务系统。 在票证创建者如何添加新评论方面存在困难 在下面的当前代码中,我正在集合中添加新记录,并将其显示为可编辑。但是,它不绑定整个模型,只绑定隐藏的idCR 我想在CRCase模型中增加一些成员来处理新的评论public CRParticipation newcrparticipation{get;set;}但我收到无效列错误,因为它不在表中 非常感谢您的指导 型号: public class CRCase { [Key] public int idCR {

希望创建一个类似的票务系统。 在票证创建者如何添加新评论方面存在困难

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

我想在CRCase模型中增加一些成员来处理新的评论
public CRParticipation newcrparticipation{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; }
}
显示和添加新注释视图:

@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")
}
@model CRManagement1.Models.CRCase
@{
ViewBag.Title=“详细信息”;
}
细节
@使用(Html.BeginForm(“NewComment”、“CRCase”)){
@Html.ValidationSummary(true)
@Html.HiddenFor(model=>model.idCR)
CRCase
@DisplayNameFor(model=>model.CRReferenceNo)
CRStatus
更新日期
评论
@foreach(Model.CRParticipations中的var项)
{
@如果(item.IdCRParticipation!=0)
{
@DisplayFor(modeleItem=>item.CRStatus)
@DisplayFor(modelItem=>item.UpdateDate)
@DisplayFor(modelItem=>item.Comments)
}
其他的
{
@EditorFor(model=>model.CRParticipations.Last().Comments)
@Html.ValidationMessageFor(model=>model.CRParticipations.Last().Comments)
}
}

} @ActionLink(“返回列表”、“索引”)

@节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
查看输出。如果你能注意到新的评论被输入,但当点击“发布”按钮时,它不会被绑定到模型中。

如果通过设置断点单步浏览视图,会发生什么情况?哪里失败了?我添加了视图输出。问题是新输入没有绑定到NewComment操作中的模型,只绑定了隐藏的idCR。CRApplications为null。我知道这听起来很傻,但你能试试@Html.EditorFor(model=>model.crparticiations[model.crparticiations.Count()-1].Comments)吗?emre,无法应用索引错误。我现在可以添加一个额外的成员,通过[NotMapped]修饰新注释来保存新注释。
@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")
}