Nir-Z
Nir-Z

Reputation: 869

MVC : System.NullReferenceException model when submit form

I've been trying to pass a model to a partial view with a form. Some of the model fields are already assigned in the GET request. When the form loads I can see the model fields values but after submiting the form I get this error in this line: @Html.Hidden("From",Model.From):

Object reference not set to an instance of an object

Why these two fields are assigned with null on submit?

My controllers:

  [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");
        }

The partial view:

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

The Model:

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

        }
    }

Thanks in advance

Upvotes: 2

Views: 7694

Answers (1)

JotaBe
JotaBe

Reputation: 39045

You're forgetting to pass the model to your view.

When you return this view, instead of this:

return PartialView("SendMessage");

you must do this:

return PartialView("SendMessage", m);

Where m is your model. That's why the model is null inside your view.

Upvotes: 6

Related Questions