0

这几天我一直在为此苦苦挣扎。我有一个表单,它是通过 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 日期转换而苦苦挣扎——我认为 MVC4 应该默认使用 JSON.NET?如果很麻烦,我会发布另一个问题。

4

1 回答 1

0

免去您的头疼并使用 jQuery。微软基本上在他们的 ASP.NET AJAX 上“跳船”,现在支持 jQuery。

http://weblogs.asp.net/toddanglin/archive/2010/04/19/microsoft-ajax-client-library-is-dead-long-live-jquery.aspx

http://stephenwalther.com/archive/2010/03/16/microsoft-jquery-and-templating.aspx

于 2012-08-07T15:38:03.813 回答