Asp.net mvc 4 MVC:提交表单时System.NullReferenceException模型

Asp.net mvc 4 MVC:提交表单时System.NullReferenceException模型,asp.net-mvc-4,Asp.net Mvc 4,我一直在尝试用表单将模型传递到局部视图。GET请求中已经分配了一些模型字段。当窗体加载时,我可以看到模型字段值,但在 提交表单时,我在这一行中遇到错误:@Html.Hidden(“From”,Model.From): 对象引用未设置为对象的实例 为什么这两个字段在提交时被赋值为null 我的控制器: [HttpGet] public ActionResult SendPrivateMessage(string from, List<string> to)

我一直在尝试用表单将模型传递到局部视图。GET请求中已经分配了一些模型字段。当窗体加载时,我可以看到模型字段值,但在 提交表单时,我在这一行中遇到错误:
@Html.Hidden(“From”,Model.From):

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

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

我的控制器:

  [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");
        }
[HttpGet]
public ActionResult SendPrivateMessage(字符串从,列表到)
{    
//其中两个字段已分配
返回PartialView(“SendMessage”,新的MessageModel(from,to));
}
[HttpPost]
公共操作结果SendPrivateMessage(MessageModel m)
{
字符串fullname=“”;
LoginModel loginData=(LoginModel)(会话[“用户]);
if(会话[“用户”]!=null)
{
fullname=loginData.LoginDS.Tables[0]。行[0][loginData.LoginDS.Tables[0]。列[“fullname”].Ordinal]。ToString();
}
m、 全名=全名;
m、 Send();
返回部分视图(“发送消息”);
}
部分观点:

@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>
@model HaifanetMobile.Models.MessageModel
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)

@Hidden(“From”,Model.From)//这就是我得到错误的地方 @Html.Hidden(“To”,Model.To)//这就是我得到错误的地方 @Html.TextBoxFor(m=>m.Subject,新的{@class=”,placeholder=“Subject:”,id=“msg_Subject”,onfocus=“this.placeholder=”,onblur=“this.placeholder=”Subject:“}) @Html.ValidationMessageFor(m=>m.Subject,“必需”) @Html.TextAreaFor(m=>m.Content,新的{@class=”,id=“msg_textarea”}) @Html.ValidationMessageFor(m=>m.Content,“必需”)

}
模型:

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

        }
    }
公共类消息模型
{
来自{get;set;}的公共字符串
要{get;set;}的公共列表
公共字符串主题{get;set;}
公共字符串内容{get;set;}
公共字符串全名{get;set;}
public MessageModel(字符串from,列表to)
{
//TODO:完成成员初始化
this.From=From;
这个;
}
公共消息模型(){
}
公共无效发送()
{
ServiceReference2.WebService1Soap ws=新的ServiceReference2.WebService1SoapClient();
if(!ws.SendMessage(this.From、this.Content、this.Subject、this.To.ToArray()、this.fullname))
抛出新异常();
}
}

提前感谢

您忘记将模型传递给视图

当您返回此视图时,而不是此视图:

return PartialView("SendMessage");
您必须这样做:

return PartialView("SendMessage", m);

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

我看不到MessageModel的任何构造函数!我编辑了模型课…谢谢…救了我一天!