MVC4使用Ajax.BeginForm不将ViewModel发送到控制器?

时间:2013-01-01 18:29:30

标签: asp.net-mvc asp.net-mvc-4 unobtrusive-javascript

这是发送到服务器的内容:

http://localhost:3182/Admin/UserAdmin/Save?Count=0&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D

我正在使用不引人注目的jquery.unobtrusive-ajax,它在我的项目中的其他页面上运行正常。我对BeginForm帮助器没有任何兴趣。实际上这很直接。如果它有帮助我可以发布它,但它也没有太多。

我认为另一个库可能会干扰我的代码,所以我删除了其他脚本,它仍然做同样的事情。这很奇怪。有没有人见过这个,知道如何修复它?

以下是整个视图:

@using YogaDiVita.Ui.Helpers
@model YogaDiVita.Domain.YogaDiVitaContext.Model.User
@{
    ViewBag.Title = "Profile";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
    var isInstructor = (bool)ViewBag.IsInstructor;

}

<link href="@Url.ContentArea("~/Scripts/plugins/fineUploader/fineuploader.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Scripts/plugins/jCrop/css/jquery.Jcrop.min.css")" rel="stylesheet"
    type="text/css" />
<div class="row-fluid">
    <div class="span12">
        <h3 class="heading">User Profile</h3>
        <div class="row-fluid">
            <div class="span8">
                @using (Ajax.BeginForm("Save",new RouteValueDictionary(), new AjaxOptions
               {
                   HttpMethod = "POST",                       
                   OnSuccess = "OnSuccess",
                   OnFailure = "OnFailure"
               }, new { @class = "form-horizontal well" }))
                {
                    @Html.HiddenFor(u => u.Id)
                    @Html.HiddenFor(u => u.CreatedById)
                    @Html.HiddenFor(u => u.ModifiedById)
                    @Html.HiddenFor(u => u.Username)
                    @Html.Hidden("isInstructor", isInstructor)
                    <fieldset>
                        <div class="control-group formSep">
                            <label class="control-label">
                                Username</label>
                            <div class="controls text_line">
                                <strong>@Model.Username</strong> <a href="/Admin/UserAdmin/ResetPassword/@Model.Id" class="btn resetPasswordWindow">Reset Password</a>
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="fileinput" class="control-label">
                                User avatar</label>
                            <div class="controls">
                                <div data-fileupload="image" class="fileupload fileupload-new">
                                    <div style="width: 80px; height: 80px;" class="fileupload-new thumbnail">
                                        <img src="http://www.placehold.it/108x108/EFEFEF/AAAAAA " alt="" id="userAvatar">
                                    </div>
                                    <a href="/Admin/UserAdmin/ImageUpload/@Model.Id" class="btn avatarUpload">Upload New
                                        Image</a>
                                </div>
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="FirstName" class="control-label">
                                First Name</label>
                            <div class="controls">
                                @Html.TextBoxFor(u => u.FirstName, new { @class = "input-xlarge" })
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="FirstName" class="control-label">
                                Last Name</label>
                            <div class="controls">
                                @Html.TextBoxFor(u => u.LastName, new { @class = "input-xlarge" })
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="u_email" class="control-label">
                                Email Address</label>
                            <div class="controls">
                                @Html.TextBoxFor(u => u.EmailAddress, new { @class = "input-xlarge" })
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="u_email" class="control-label">
                                Telephone Number</label>
                            <div class="controls">
                                @Html.TextBoxFor(u => u.TelephoneNumber, new { @class = "input-xlarge" })
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="u_email" class="control-label">
                                Mobile Number</label>
                            <div class="controls">
                                @Html.TextBoxFor(u => u.MobileTelephoneNumber, new { @class = "input-xlarge" })
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="u_email" class="control-label">
                                Mobile Number</label>
                            <div class="controls">
                                @Html.TextBoxFor(u => u.MobileTelephoneNumber, new { @class = "input-xlarge" })
                            </div>
                        </div>
                        <div class="control-group formSep">
                            <label for="u_email" class="control-label">
                                Is Instructor</label>
                            <div class="controls">
                                @Html.CheckBox("cbIsInstructor", isInstructor)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="controls">
                                <button class="btn btn-gebo" type="submit">
                                    Save changes</button>

                            </div>
                        </div>
                    </fieldset>

                }
            </div>
        </div>
    </div>
</div>

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"
    type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/plugins/jcrop/jquery.Jcrop.min.js")" type="text/javascript"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/util.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/button.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/handler.base.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/handler.form.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/handler.xhr.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/uploader.basic.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/dnd.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/uploader.js")"></script>
<script src="@Url.ContentArea("~/Scripts/plugins/fineUploader/jquery-plugin.js")"></script>



<script>
    $(function () {

        loadUserAvatar();
        $('.avatarUpload').colorbox({
            initialHeight: '520',
            initialWidth: '650',
            iframe: false,
            opacity: 0.45,
            onClosed: function () {
                loadUserAvatar();
            }
        });


        $('.resetPasswordWindow').colorbox({
            initialHeight: '0',
            initialWidth: '0',
            iframe: false,
            opacity: 0.45,
            onClosed: function () {

            },
            onComplete: function () {

                $.validator.addMethod("passwordsMustMatch", function (value, element) {
                    return $('#validatePassword').val() == $('#password').val();
                }, "The passwords do not match");

                $('.resetPasswordForm').validate({
                    onkeyup: false,
                    errorClass: 'error',
                    validClass: 'valid',
                    errorPlacement: function (error, element) {
                        error.appendTo(element.closest("div.controls"));
                    },
                    highlight: function (element) {
                        $(element).closest("div.control-group").addClass("error f_error");
                        var thisStep = $(element).closest('form').prev('ul').find('.current-step');
                        thisStep.addClass('error-image');
                    },
                    unhighlight: function (element) {
                        $(element).closest("div.control-group").removeClass("error f_error");
                        if (!$(element).closest('form').find('div.error').length) {
                            var thisStep = $(element).closest('form').prev('ul').find('.current-step');
                            thisStep.removeClass('error-image');
                        };
                    },
                    rules: {
                        password: { required: true, minlength: 6, passwordsMustMatch: true },
                        validatePassword: { required: true, minlength: 6, passwordsMustMatch: true }
                    },
                    invalidHandler: function (form, validator) {
                        $.sticky("There are some errors. Please corect them and submit again.", { autoclose: 5000, position: "top-right", type: "st-error" });
                    }
                });
            }
        });

    });


    function loadUserAvatar(parameters) {
        $.ajax({
            url: '/Admin/Avatar/AvatarLoad/@Model.Id',
            type: 'POST',
            cache: false,
            timeout: 100000,
            error: function (xhr, status, error) {
                alert(error + " " + status);
            },
            success: function (data) {
                $("#userAvatar").attr("src", data.Image.ThumbNailRelativePath);
            }

        });
    }

    function OnSuccess(parameters) {
        $.sticky("The user profile has been updated successfully.", { autoclose: 5000, position: "top-right", type: "st-error" });
    }
    function OnFailure(parameters) {
        $.sticky("There was an error saving the profile. </br>" + parameters.message, { autoclose: 5000, position: "top-right", type: "st-error" });
    }
</script>

更新

在对所发生的事情进行了一些研究之后,我在帖子后发现了这个错误:An item with the same key has already been added

2 个答案:

答案 0 :(得分:0)

您无法直接绑定到收藏集 - 请参阅ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries了解如何完成您要执行的操作的示例。

答案 1 :(得分:0)

好吧,我想出了这个问题。有点傻。问题是我的模型上有一个界面。有2个版本的属性用户名。一个名为UserName(大写为N),另一个名为Username(小写n)。 This blog post helped