MVC绑定列表的对象问题

时间:2014-03-09 12:47:07

标签: forms asp.net-mvc-4

关注我的上一篇文章MVC model property null on posting form

通过查看示例,我了解当我需要发布

从视图/ partialView列表到控制器我应该遍历列表并使用

@HiddenFor属性和帖子,MVC可以将这些字段解析为列表。

不知何故控制器甚至没有被调用,只有当我删除这些行

      @for (int i = 0; i < Model.To.Count; i++)
        {

             @Html.HiddenFor(m => m.To[i].UserName)
             @Html.HiddenFor(m => m.To[i].FirstName)
             @Html.HiddenFor(m => m.To[i].LastName)


        } 

正在调用控制器但是发生了同样的问题,我的列表为空。

我的观点:

@model Mobile.Models.MessageModel

<script type="text/javascript">



    $(function () {
        $('form').submit(function () {

            $.validator.unobtrusive.parse($('form'));  //added

            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        alert("message sent");
                        $('#messagedetails_panel').toggle();
                        $("#messages_panel").show();
                        $("#BtnSuccessMsg").click();
                    },
                    error: function (xhr, textStatus, errorThrown) {

                        alert("message failed");
                    }
                });
            }
            return false;
        });
    });
</script>




    <table id="messages_panel_mbar" cellpadding="0" cellspacing="0">

    <tr>
        <td class="left_mbar">

        </td>
        <td class="main_mbar"> 
        </td>
        <td id="back_msg_details" class="right_mbar"></td>
    </tr>
</table>


    <div style="height:10%; width:100%; font: bold; font-size: 20px; text-align:right;"> @Html.Raw(Model.Subject)</div>
    <div id="msg_chat" style="text-align:right; width:100%; height:auto; max-height:80%; overflow-y:scroll;">
          @Html.Raw(Model.MsgHistory)
    </div>
    <div id="reply_msg" style="height: 5%">reply</div>

    <div id="reply_msg_block" class="visible" style="width:100%; height:45%;">

        @using (Ajax.BeginForm("ReplyMessage", "SettingsMenu", null, new AjaxOptions { }, new { @class = "center_form" }))
        {


            @Html.ValidationSummary(true, "");

            <br />

            <fieldset style="height:75%">




                @Html.HiddenFor(m => m.Subject)
                @Html.HiddenFor(m => m.ParentId)
                @Html.HiddenFor(m => m.From)
                @Html.HiddenFor(m => m.fullnamesender)

                @for (int i = 0; i < Model.To.Count; i++)
                {

                     @Html.HiddenFor(m => m.To[i].UserName)
                     @Html.HiddenFor(m => m.To[i].FirstName)
                     @Html.HiddenFor(m => m.To[i].LastName)


                }


                <div id="textarea_msg_reply">
                        @Html.TextAreaFor(m => m.Content, new { @class = "" })
                        @Html.ValidationMessageFor(m => m.Content)
                </div>

            </fieldset>

            <input type="submit" value="send" />


        }

    </div>

我的控制器:

  [HttpPost]
        public ActionResult ReplyMessage(MessageModel model)// List<Contacts> is empty

        {


            var errors = ModelState.Values.SelectMany(v => v.Errors);
            if (ModelState.IsValid)
            {
                try
                {
                    model.Send();
                }
                catch
                {
                    throw new HttpException(404, "Error");

                }

            }


        throw new HttpException(404, "Error");
    }

请指教!谢谢!

1 个答案:

答案 0 :(得分:0)

好的,所以我明白了。 我做得很好,除了Contact类没有无参数构造函数。 我的观点:

@model Mobile.Models.MessageModel

<script type="text/javascript">
    function showMessagesPanel1() {

        $('#messagedetails_panel').toggle();
        $("#messages_panel").show();
    }

    $('#back_msg_details').click(function () {

        $.ajax({
            url: '@Url.Action("Messages", "SettingsMenu")',
            method: 'GET',
            success: function (data) {
                $("#messages_list").empty();
                $("#messages_list").html(data);
                $('#messagedetails_panel').toggle();
                $("#messages_panel").show();
            }
        });

    })


    $('#reply_msg').click(function () {
        if ($("#reply_msg_block").is(":visible")) {

            $('#msg_chat').animate({ height: '80%' });
            // $("#reply_msg_block").slideUp().removeClass('visible');

        }
        else {

            $('#msg_chat').animate({ height: '35%' });
        }
        $("#reply_msg_block").animate({ heigh: 'toggle' }, 350);
    })


    $(function () {
        $('form').submit(function () {

            $.validator.unobtrusive.parse($('form'));  //added

            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        alert("message sent");
                        $('#messagedetails_panel').toggle();
                        $("#messages_panel").show();
                        $("#BtnSuccessMsg").click();
                    },
                    error: function (xhr, textStatus, errorThrown) {

                        $('#reply_msg_block').html(xhr.responseText);
                    }
                });
            }
            return false;
        });
    });
</script>




    <table id="messages_panel_mbar" cellpadding="0" cellspacing="0">

    <tr>
        <td class="left_mbar">

        </td>
        <td class="main_mbar"> 
        </td>
        <td id="back_msg_details" class="right_mbar"></td>
    </tr>
</table>


    <div style="height:10%; width:100%; font: bold; font-size: 20px; text-align:right;"> @Html.Raw(Model.Subject)</div>
    <div id="msg_chat" style="text-align:right; width:100%; height:auto; max-height:80%; overflow-y:scroll;">
          @Html.Raw(Model.MsgHistory)
    </div>
    <div id="reply_msg" style="height: 5%">reply</div>

    <div id="reply_msg_block" class="visible" style="width:100%; height:45%;">

        @using (Ajax.BeginForm("ReplyMessage", "SettingsMenu", null, new AjaxOptions { }, new { @class = "center_form" }))
        {


            @Html.ValidationSummary(true, "");

            <br />

            <fieldset style="height:75%">




                @Html.HiddenFor(m => m.Subject)
                @Html.HiddenFor(m => m.ParentId)
                @Html.HiddenFor(m => m.From)
                @Html.HiddenFor(m => m.fullnamesender)

                @for (int i=0; i<Model.To.Count; i++ )
                {

                    @Html.HiddenFor(m => m.To[i].UserName)
                    @Html.HiddenFor(m => m.To[i].FirstName)
                    @Html.HiddenFor(m => m.To[i].LastName)

                }


                <div id="textarea_msg_reply">
                        @Html.TextAreaFor(m => m.Content, new { @class = "" })
                        @Html.ValidationMessageFor(m => m.Content)
                </div>

            </fieldset>

            <input type="submit" value="send" />


        }

    </div>