【问题标题】:MVC : System.NullReferenceException model when submit formMVC:提交表单时的 System.NullReferenceException 模型
【发布时间】:2014-01-21 18:12:30
【问题描述】:

我一直在尝试将模型传递给带有表单的局部视图。某些模型字段已在 GET 请求中分配。当表单加载时,我可以看到模型字段值,但之后 提交表单我在这一行收到此错误:@Html.Hidden("From",Model.From):

对象引用未设置为对象的实例

为什么这两个字段在提交时被赋值为空?

我的控制器:

  [HttpGet]
        public ActionResult SendPrivateMessage(string from, List<string> to)
        {    
            // two of the fields are already assigned
            return PartialView("SendMessage", new MessageModel(from,to));
        }

        [HttpPost]
        public ActionResult SendPrivateMessage(MessageModel m)
        {
            string fullname = "";
            LoginModel loginData = (LoginModel)(Session["user"]);
            if (Session["user"] != null)
            {
                 fullname = loginData.LoginDS.Tables[0].Rows[0][loginData.LoginDS.Tables[0].Columns["fullname"].Ordinal].ToString();
            }
            m.fullname = fullname;
            m.Send();
            return PartialView("SendMessage");
        }

局部视图:

@model HaifanetMobile.Models.MessageModel   
<div id="contact_form">

    <a id="back_contact" href="#" style="float:left">
        <img style="height:20px; width:30px;" src="~/Images/back_btn.gif" alt="back" />.
    </a>
    <div id="contactus_title">
        <div id="close_contactus" style="float:right"><img style="height:20px; width:20px;" src="~/Images/close_btn.gif" /></div>
    </div>

   @using (Html.BeginForm())
    {
       @Html.ValidationSummary(true)    
        <br />           
       <fieldset>           
           @Html.Hidden("From", Model.From) //this is where I get the error
           @Html.Hidden("To", Model.To)//this is where I get the error
       <div>
            @Html.TextBoxFor(m => m.Subject, new { @class = "", placeholder = "subject:", id = "msg_subject", onfocus = "this.placeholder = ''", onblur = "this.placeholder = 'subject:'" })
            @Html.ValidationMessageFor(m => m.Subject, "required")
        </div>

        <div>
            @Html.TextAreaFor(m => m.Content, new { @class = "", id = "msg_textarea" })
            @Html.ValidationMessageFor(m => m.Content, "required")
        </div>
      </fieldset>
            <p>
              <input type="submit" value="send" />
            </p>
    }   
</div>

模型:

public class MessageModel
    {
        public string From { get; set; }
        public List<string> To { get; set; }
        public string Subject {get; set;}
        public string Content { get; set; }
        public string fullname { get; set; }




        public MessageModel(string from,  List<string> to)
        {

            // TODO: Complete member initialization
            this.From = from;
            this.To = to; ;
        }
        public MessageModel() {


        }

        public void Send()
        {

            ServiceReference2.WebService1Soap ws = new ServiceReference2.WebService1SoapClient();
            if (!ws.SendMessage(this.From, this.Content, this.Subject, this.To.ToArray() ,this.fullname))
                throw new Exception();

        }
    }

提前致谢

【问题讨论】:

  • 我看不到 MessageModel 的任何构造函数!
  • 我编辑了模型类...

标签: asp.net-mvc-4


【解决方案1】:

您忘记将模型传递给您的视图。

当你返回这个视图时,而不是这个:

return PartialView("SendMessage");

你必须这样做:

return PartialView("SendMessage", m);

m 是您的模型。这就是模型在您的视图中为空的原因。

【讨论】:

  • 谢谢...拯救了我的一天!
猜你喜欢
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
相关资源
最近更新 更多