Ajax.BeginForm()不通过ajax提交

时间:2012-08-07 10:34:56

标签: razor asp.net-ajax asp.net-mvc-4

我这几天一直在努力。我有一个表单是通过Ajax加载的部分视图,但它不是通过Ajax提交,即使它使用Ajax.BeginForm()。

控制器:

<HttpGet()> _
    Public Function PersonalDetailsEdit(PersonalInfo As DetailsViewModel.PersonalDetails) As PartialViewResult
        Return PartialView(PersonalInfo)
    End Function

    <HttpPost()> _
    <AjaxOnly()> _
    Public Function PersonalDetailsEdit(formcollection As FormCollection, model As DetailsViewModel.PersonalDetails) As PartialViewResult
        Return PartialView("PersonalDetails", model)
    End Function

查看:

@ModelType ASK.Everest.ViewModels.DetailsViewModel.PersonalDetails

@Using Ajax.BeginForm("PersonalDetailsUpdate", "Details", New AjaxOptions() With {
                     .InsertionMode=InsertionMode.Replace,
                     .UpdateTargetId = "PersonalDetailsSection",
                     .OnFailure = "alert('fail');"}, New With {.id="PersonalDetailsForm"})
    @Html.ValidationSummary(True)

@<fieldset id="PersonalDetailsUpdate">
    <table class="table table-striped ">
        <tr>
            <th class="display-label">
                <input type="hidden" value="@Html.NameFor(Function(model) model.Title)" />
                @Html.DisplayNameFor(Function(model) model.Title)
            </th>
            <th class="display-label">
                <input type="hidden" value="@Html.NameFor(Function(model) model.FirstName)" />
                @Html.DisplayNameFor(Function(model) model.FirstName)
            </th>
            <th class="display-label">
                <input type="hidden" value="Surname" />
                @Html.DisplayNameFor(Function(model) model.Surname)
            </th>
        </tr>
        <tr>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.Title)
            </td>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.FirstName)
            </td>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.Surname)
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <th class="display-label">
                @Html.DisplayNameFor(Function(model) model.Gender)
            </th>
            <th class="display-label">
                <input type="hidden" value="@Html.NameFor(Function(model) model.DOB)" />
                @Html.DisplayNameFor(Function(model) model.DOB)
            </th>
        </tr>
        <tr>
            <td>
            </td>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.Gender)
            </td>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.DOB)
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <th class="display-label" colspan="2">
                @Html.DisplayNameFor(Function(model) model.IDNumber)
            </th>
        </tr>
        <tr>
            <td>
            </td>
            <td class="display-field" colspan="2">
                @Html.DisplayFor(Function(model) model.IDNumber)
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <th class="display-label">
                @Html.DisplayNameFor(Function(model) model.MaritalStatus)
            </th>
            <th class="display-label">
                @Html.DisplayNameFor(Function(model) model.Nationality)
            </th>
        </tr>
        <tr>
            <td>
            </td>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.MaritalStatus)
            </td>
            <td class="display-field">
                @Html.DisplayFor(Function(model) model.Nationality)
            </td>
        </tr>
    </table>
    @*@Html.ActionLink("Back to List", "Index")*@
    <p class="pull-right">

        <input type="submit" value="Save" />
    </p>
</fieldset>
End Using

我的捆绑包:

bundles.Add(New ScriptBundle("~/bundles/ask").Include(
                "~/Scripts/jquery-1.7.*",
                "~/Scripts/jquery-ui*",
                "~/Scripts/jquery.validate*",
                "~/Scripts/jquery.unobtrusive*",
                "~/Content/bootstrap/js/bootstrap.js"))

_Layout.vbtml:

<!DOCTYPE html>
<html lang="eng">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewData("Title")</title>
    @Styles.Render("~/Content/css")
    @RenderSection("styles", required:=False)

    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/ask")

    @RenderSection("scripts", required:=False)
</head>
<body>
    @RenderBody()
</body>
</html>

经过多次挫折后放弃了,并决定在客户端使用MVVM进行JQuery + Knockout路由,如下所示:

<script type="text/javascript">
var data = @Html.Raw(Json.Encode(Model)) ;
var PersonalDetailsVM = ko.mapping.fromJS(data);
ko.applyBindings(PersonalDetailsVM, $('section#PersonalDetails')[0]);
</script>

现在我只是在努力处理JSON / MVC Date转换 - 我认为MVC4默认应该使用JSON.NET?如果有很多麻烦,我会发布另一个问题。

1 个答案:

答案 0 :(得分:0)