AccessViolationException was unhandled

末鹿安然 提交于 2019-12-05 02:38:37

Seems to me that you're working harder than you need to.

First, replace the foreach with a for loop, passing the indexed element into an editor template. This will establish your template context.

<fieldset>
    <legend>Relationship Codes</legend>
    <table id="Experiment">
        <thead>
            <tr>
                <th>Relationship Effective Date</th>
                <th>Relationship Dist Code</th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.AgentRelationshipCodes.Count(); i++)
            {
                @Html.EditorFor(model => model.AgentRelationshipCodes[i])
            }
        </tbody>
    </table>
    <br/>
    <a href="javascript:void(0)" class ="addCode">Add Another</a>
</fieldset>

Then create an editor template called AgentRelationshipCodes.cshtml (in Views/Shared/EditorTemplates)

@model Monet.Models.AgentRelationshipCodes
<tr>
    <td>@Html.EditorFor(model => model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
    <td>@Html.EditorFor(model => model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.RelCodeOrdinal)
</tr>

This eliminates the need for the custom helpers which are what appear to be causing the problem.

Lastly, for adding new elements - move the fieldset to a partial:

<script>
    $(document).ready(function() {
       $(".addCode").click(function () {
            $('#fieldset').load('@Url.Action("NewRelationshipCode", "AgentTransmission")',$('#fieldset').closest('form').serialize());
       });
    });
</script>

<div id="fieldset">
   @Html.Partial("fieldset");
</div>

And return the fieldset view from your NewRelationshipCode action method:

[HandleProcessCorruptedStateExceptions]
public ViewResult NewRelationshipCode(YourViewModel model)
{
    model.AgentRelationshipCodes.Add(new AgentRelationshipCodes());
    return View("fieldset", model);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!