发布表单上的MVC模型属性为null

时间:2014-03-05 12:12:52

标签: asp.net-mvc-4 model

我正在开发一个MVC 4应用程序,我在使用强视图模型提交表单时遇到问题。

我正在调用部分视图,在GET actionResult中我声明一个模型实例并将其传递给局部视图并将字段存储在隐藏字段中。 调试时,我看到所有字段都获得了值,并且工作正常。

用户提交表单后,我使用此模型属性以及用户填充的属性。

问题是在调用post动作结果时To列表变为null。

控制器:

   [HttpGet]
    public ActionResult MessageDetails(string id)
    {
        MessageModel m;
        string userfullname =  String.Empty;

        ServiceReference2.WebService1Soap ws = new ServiceReference2.WebService1SoapClient();
        DataTable dt = ws.GetMessageDetails("bcce7f7ad7596f963f4adb23d713e0d4", "329392de8a55edf86c0881a57381cbe6",id,User.Identity.Name).Tables[0];

        if (Session["user"] != null)
            userfullname = ((HaifanetMobile.Models.LoginModel)Session["user"]).LoginDS.Tables[0].Rows[0][((HaifanetMobile.Models.LoginModel)Session["user"]).LoginDS.Tables[0].Columns["fullname"].Ordinal].ToString();

        m = new MessageModel(dt, User.Identity.Name, userfullname);



        return PartialView("MessageDetails",m);
    }


    [HttpPost]
    public ActionResult ReplyMessage(MessageModel model)
    {

     // here's where the To list is getting empty
        var errors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {

            model.Send();
            return Content("Success");
        }


        return PartialView("MessageDetails",model);
    }

观点:

@model HaifanetMobile.Models.MessageModel

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

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


    $(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");

                    },
                    error: function () {
                        alert("Error");
                    }
                });
            }
            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, "");


            <fieldset style="height:75%">



                @Html.Hidden("Subject", Model.Subject)
                @Html.Hidden("ParentId", Model.ParentId)
                @Html.Hidden("From", Model.From)
                @Html.Hidden("fullnamesender", Model.fullnamesender)
                @Html.Hidden("To", Model.To)


                <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>

模特:

 public class MessageModel
    {
        public string From { get; set; }

        [Required(ErrorMessage = "Enter To")]
        public List<Contact> To { get; set; }

        [Required(ErrorMessage = "Enter Subject")]
        [StringLength(100, ErrorMessage = "Max 1000 chars")]
        public string Subject {get; set;}

        [StringLength(1000, ErrorMessage = "Max 1000 chars")]
        public string Content { get; set; }

        public string MsgHistory { get; set; }

        public string fullnamesender { get; set; }

        public string ParentId { get; set; }

        public DateTime LastMsgDate { get; set; }

        public bool readed = false;


} // I omitted the constructors....

1 个答案:

答案 0 :(得分:1)

看看这个问题和答案。

Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?

HiddenFor通常更容易使用,但它本身并不能解决您的问题。您应该为回发后要使用的每个列表项的每个属性设置一个隐藏。

然而,我不建议这种做法。您应该只为原始邮件的标识符放置一个隐藏字段,并在回发后查找收件人列表。