AccessViolationException未处理

时间:2014-05-02 22:16:37

标签: c# asp.net-mvc asp.net-mvc-3 access-violation begincollectionitem

我尝试使用Steve Sanderson's blog post来编辑ASP MVC 3视图中的可变长度列表。项目构建正常,但是无论何时呈现局部视图,程序都会在using(Html.BeginColletionItem()行上出现此错误:

AccessViolationException was unhandled
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

这是完整例外的屏幕截图

enter image description here

完整的堆栈跟踪

at Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Connection conn)
at Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(Object acceptedSocket)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

部分视图

@model Monet.Models.AgentRelationshipCodes


@using (Html.BeginCollectionItem("AgentRelationshipCodes")) @*Exception thrown here*@
{
    <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>
}

查看

    <script>
    $(document).ready(function() {
        $(".addCode").click(function () {
                $.ajax({
                url: '@Url.Action("NewRelationshipCode", "AgentTransmission")',
                dataType: 'html',
                cache: false,
                success: function (html) {
                    console.log(html);
                    $("#Experiment > tbody").append(html);
                }
            })
        });
    });
    </script>
    .
    .
<fieldset>
    <legend>Relationship Codes</legend>
    <table id="Experiment">
        <thead>
            <tr>
                <th>Relationship Effective Date</th>
                <th>Relationship Dist Code</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.AgentRelationshipCodes)
            {
                @Html.Partial("AddRelationshipCodePartial", item)
            }
        </tbody>
    </table>
    <br/>
    <a href="javascript:void(0)" class ="addCode">Add Another</a>
</fieldset>

控制器

    [HandleProcessCorruptedStateExceptions]
    public ViewResult NewRelationshipCode()
    {
        return View("AddRelationshipCodePartial", new AgentRelationshipCodes());
    }

AgentRelationshipCodes

namespace Monet.Models
{
    using System;
    using System.Collections.Generic;

    public partial class AgentRelationshipCodes
    {
        public int ID { get; set; }
        public int RelCodeOrdinal { get; set; }
        public string RelationshipId { get; set; }
        public Nullable<System.DateTime> EffectiveDate { get; set; }
        public System.DateTime LastChangeDate { get; set; }
        public string LastChangeId { get; set; }

        public virtual AgentTransmission AgentTransmission { get; set; }
    }
}

修改

我已经能够在我现在正在使用的解决方案之外的项目中使用该演示,所以它显然与此工作区中的某些dll有关。但是,现在我高于我的薪水,因为我不确定如何调试这样的东西。以下是在Visual Studio抛出AccessViolationException之前由WinDbg标识的异常。抛出异常之间有很多信息,如果有人需要,请告诉我。

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\d12f4fda3d1bfabf888342e96983e9a7\mscorlib.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\d12f4fda3d1bfabf888342e96983e9a7\mscorlib.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xaml\9d3572e8c3c314a0f12383d41e8bee78\System.Xaml.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xaml\9d3572e8c3c314a0f12383d41e8bee78\System.Xaml.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\Presentatio5ae0f00f#\8711b01d60a94d6ef6a02d7fd0578493\PresentationFramework.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\Presentatio5ae0f00f#\8711b01d60a94d6ef6a02d7fd0578493\PresentationFramework.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\ac2e26bafa70e93b307087d7fe6b9dd2\WindowsBase.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\ac2e26bafa70e93b307087d7fe6b9dd2\WindowsBase.ni.dll

*** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\Microsoft.V4e91a071#\207156ac71b58fb31310a2f78c3d0c44\Microsoft.VisualStudio.Web.Application.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\Microsoft.V4e91a071#\207156ac71b58fb31310a2f78c3d0c44\Microsoft.VisualStudio.Web.Application.ni.dll

更新

选择&#34; Native Code&#34;项目调试器菜单中的选项

enter image description here

我现在收到略微更详细的错误消息:

enter image description here

最后,按照以下建议切换到IIS Express,我仍然收到AccessViolationException。以下是我用于启用IIS进行调试的设置(在项目属性下)

enter image description here

以下是错误消息

enter image description here

调用堆栈:

enter image description here

1 个答案:

答案 0 :(得分:1)

对我来说,你的工作比你需要的更努力。

首先,用for循环替换foreach,将索引元素传递给编辑器模板。这将建立您的模板上下文。

<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>

然后创建一个名为AgentRelationshipCodes.cshtml的编辑器模板(在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>

这消除了对导致问题的自定义助手的需求。

最后,添加新元素 - 将字段集移动到部分:

<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>

并从NewRelationshipCode操作方法返回字段集视图:

[HandleProcessCorruptedStateExceptions]
public ViewResult NewRelationshipCode(YourViewModel model)
{
    model.AgentRelationshipCodes.Add(new AgentRelationshipCodes());
    return View("fieldset", model);
}
相关问题