【问题标题】:mvc 3 partial view with Ajax.BeginForm not working (posting to new form arrgh)带有 Ajax.BeginForm 的 mvc 3 部分视图不起作用(发布到新表单 arrgh)
【发布时间】:2011-05-05 01:50:15
【问题描述】:

我有一个包含 2 列的 mvc 3 表单。 The left column is a treeview and when a node is selected the div with id='partialView' is updated to show the details of that node.这似乎工作正常。 编辑表单(即部分视图)加载到 id='partialView' 的 div 中。

现在当用户提交此部分视图时会出现问题......现在它确实回发到控制器和正确的方法但是结果没有返回到 id='partialView' 的 div 而是发布到新页面.

所以这是我希望部分视图发布并返回替换现有部分视图的场景。

这可能吗?

我在下面包含我的代码以作为我的部分视图...

@model DataModel.Code



@using (Ajax.BeginForm("Edit", "Code", new AjaxOptions {
                                    UpdateTargetId = "partialView", 
                                    HttpMethod="POST"

                                    }   
                                    )) {    


    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Code</legend>


        @Html.HiddenFor(model => model.CodeID)

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-field">
            @Html.EditorFor(model => model.Note)
            @Html.ValidationMessageFor(model => model.Note)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DateModified)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateModified)
            @Html.ValidationMessageFor(model => model.DateModified)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.TopicID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.TopicID)
            @Html.ValidationMessageFor(model => model.TopicID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Special)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Special)
            @Html.ValidationMessageFor(model => model.Special)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Html)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Html)
            @Html.ValidationMessageFor(model => model.Html)
        </div>

        <p>
            <input type="submit" value="Save" />

        </p>



    </fieldset>
}



<div>
    @Html.ActionLink("Back to List", "Index")
</div>


@Html.Telerik().ScriptRegistrar().jQuery(true)

<script type="text/javascript">

    $(document).ready(function () {



    });


</script>

【问题讨论】:

  • 您提交的操作是返回部分视图还是完整视图?编辑:我刚刚注意到您没有在局部视图中包含 jquery 脚本。尝试添加它们。
  • 我返回一个 PartialView 但是它返回到一个新页面(不是 div partialView)。我已将脚本添加到页面中,它没有任何区别。事实上,脚本也在主页中。

标签: asp.net-mvc-3


【解决方案1】:

包括

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> 

在您的 _Layout.cshtml 中

【讨论】:

  • 我在这个完全相同的问题上花了半个工作日,因为我的部分视图没有调用具有此行的主文件。谢谢。
  • 哈,不错。我从其他人那里继承了一些代码 - 我看到它包含 unobtrusive-ajax.js,我没有质疑它......结果是的,只是那个包含被注释掉了。去死吧!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 2017-01-28
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
相关资源
最近更新 更多