MVC Razor Ajax表单提交'意外令牌u'

时间:2013-09-06 22:34:27

标签: javascript jquery asp.net asp.net-mvc razor

这是一个MVC VB.NET Razor应用程序。我有一个局部视图,它加载在父视图的底部。在该局部视图中,我有一些按钮,当点击时会弹出一个弹出对话框模态窗口,该窗口附有一个局部视图。用户应该能够编辑表单然后单击更新,然后将信息发布到控制器。但是我在提交时收到以下错误消息。

enter image description here

我关注了博客here,以便将所有内容都连接起来。单击更新按钮时,此处出现错误:

enter image description here

下面是PartialView,其中包含触发弹出模式的按钮和javascript

@ModelTYPE IEnumerable(of data_manager.attendance)

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascrip</script>
<table>
<tr>
    <th>Conf. Number</th>
    <th>Class Title</th>
    <th>Status of Class</th>
    <td>Edit</td>
</tr>    
@For Each x In Model
    Dim currentItem = x
    @<tr>
    <td>@Html.DisplayFor(Function(f) currentItem.conf_number)</td>
    <td>@Html.DisplayFor(Function(f) currentItem.courseTitle)</td>
    @If currentItem.Completed_Class = "Completed" Then
        @<td>@Html.ActionLink("Completed(Print Cert)", "Ind_Cert", "Printing", New With {.firstName = currentItem.firstName, .lastname = currentItem.lastName, .classRef = currentItem.course_ref, .cNumber = currentItem.conf_number}, Nothing)</td>
    Else
        @<td>@Html.DisplayFor(Function(f) currentItem.Completed_Class)</td> 
    End If
    <td>@Html.ActionLink("Modify", "CourseHistoryEdit", New With {.id = currentItem.id}, New With {.class = "editLink"})</td>
    </tr>
Next
</table>
<div id="updateDialog" title="Update Attendance"></div>

<script type="text/javascript">
var linkObj;
$(function () {
    $(".editLink").button();
    $('#updateDialog').dialog({
        autoOpen: false,
        width: 400,
        resizable: false,
        modal: true,
        buttons: {
            "Update": function () {
                $("#update-message").html(''); //make sure there is nothing on the message before we continue                         
                $("#updateAttendance").submit();
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });
    $(".editLink").click(function () {
        //change the title of the dialgo
        linkObj = $(this);
        var dialogDiv = $('#updateDialog');
        var viewUrl = linkObj.attr('href');
        $.get(viewUrl, function (data) {
            dialogDiv.html(data);
            //validation
            var $form = $("#updateAttendance");
            // Unbind existing validation
            $form.unbind();
            $form.data("validator", null);
            // Check document for changes
            $.validator.unobtrusive.parse(document);
            // Re add validation with changes
            $form.validate($form.data("unobtrusiveValidation").options);
            //open dialog
            dialogDiv.dialog('open');
        });
        return false;
    });
});

function updateSuccess(data) {
    if (data.Success == true) {
        //we update the table's info
        var parent = linkObj.closest("tr");
        parent.find(".Completed_Class").html(data.Object.completed);
        parent.find(".carDescription").html(data.Object.Description);
        //now we can close the dialog
        $('#updateDialog').dialog('close');
        //twitter type notification
        $('#commonMessage').html("Update Complete");
        $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}
</script>

这是在每个旁边单击“修改”按钮时呈现的partialView。

   @ModelTYPE DataModels.DataModels.AjaxCourseHistoryEdit

   @Using (Ajax.BeginForm("CourseHistoryEdit", "Admin", Nothing, New AjaxOptions With {.InsertionMode = InsertionMode.Replace, .HttpMethod = "POST", .OnSuccess = "updateSuccess"}, New With {.id = "updateAttendance"}))
   @Html.ValidationSummary(true)

   @<fieldset>
   <legend>Attendance Update</legend>
   @Html.HiddenFor(Function(m) Model.attendId) 
   <div class="editor-label">
    @Html.Label("Course Title")
   </div>
   <div class="editor-field">
    @Html.DisplayFor(Function(m) Model.courseTitle)
   </div>
   <div class="editor-label">
    @Html.Label("Completed Status")
   </div>
   <div class="editor-field">
     @Html.DropDownList("completed", New SelectList(ViewBag.CourseStatuses))
   </div>
   <div class="editor-label">
    @Html.Label("Hours Completed")
   </div>
   <div>
    @Html.EditorFor(Function(m) Model.hoursCompleted)
   </div>
   </fieldset>    
   End Using

以下是正在项目的_layout文件中加载的javascript库。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript">     </script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

非常感谢任何帮助。我已经花了好几个小时,谷歌搜索已经发现几个SO帖子说Unexpected token u与无效的线路终止有关。这对我没有帮助,因为我找不到任何远程看起来不合适的HTML,即没有关闭的标签..

我有一个csharper在表和fieldset上调出@。在这些情况下这是正常的,对于vb.net,下面是渲染的html的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:2)

Moeri的评论指出了我正确的方向。事实证明我的模型使用hiddenFor值的整数值。由于我不知道的原因,AJAX帖子根本不喜欢这样。通过将attendId的类型从Integer更改为String并进一步使用正确的editorFor / labelFor,问题已得到解决。也许这会帮助那些像我一样打击这个绊脚石的人。