0

Hello everyone i have made a entity and i have made a view using a model, "clients" now i have tried sending an email via the fields entered in the view this is what i have, for security reasons i have only used one field for a test and the port numbers, host etc is random text

the view:

  @model _2.Models.Client
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
    <fieldset>
            <legend>Client</legend>
    <div class="editor-label">
                @Html.LabelFor(model => model.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
            </div>
<div class="editor-label">
                @Html.LabelFor(model => model.Initials)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Initials)
                @Html.ValidationMessageFor(model => model.Initials)
            </div>
<div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
      <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

Controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(Client model)
        {
            try
            {


                if (ModelState.IsValid)
                {

                    var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                    var message = new MailMessage();
                    message.To.Add(new MailAddress("test.co.za"));
                    message.To.Add(new MailAddress("test.co.za")); 
                    message.From = new MailAddress("test.co.za"); 
                    message.Subject = "Your email subject";
                    message.Body = string.Format(body, model.Title, model.Initials, model.FirstName);

                    message.IsBodyHtml = true;

                    using (var smtp = new SmtpClient())
                    {

                        {
                            smtp.Host = "host";
                            smtp.Port = port number;
                            await smtp.SendMailAsync(message);
                            smtp.Dispose();
                            return RedirectToAction("Sent");
                        };



                    }

                }
                return View(model);
            }

            catch (NullReferenceException ex)
            {
              return Content("Data added successfully" + ex); 
            }
        }


        public ActionResult Sent()
        {
            return View();
        }

That is the error i get

System.NullReferenceException: Object reference not set to an instance of an object

it is picking up null values even though i have entered text in the fields, please help me resolve this thank you for your time i have created an instance of client but it just sends blank emails with the body text, it did not send the values entered in the editor fields

Julius
  • 31
  • 1
  • 7
  • Delete smtp.Dispose(); It is not the cause but the using statement will take care of disposing the resources – Julius Depulla Dec 01 '15 at 21:23
  • Ensure your model which is Client is the same in the view . – Julius Depulla Dec 01 '15 at 21:25
  • public class Client { public string Title {get; set;} public string Initials { get; set;} public string FirstName { get; set; } – Julius Depulla Dec 01 '15 at 21:27
  • In View, @model Client; with right namespace added at the top of the view page – Julius Depulla Dec 01 '15 at 21:28
  • Then,
    @Html.EditorFor(m => m.Title)
    – Julius Depulla Dec 01 '15 at 21:28
  • thank you for replying, the class client already has the title, firstname, initials get and set, sorry i never specify that, the view, "@model" is correct, an the "@html.editorfor" is correct because visual studios picks it up, it acknowledges that there is Get & set for those values, it even shows that its being referenced, so i dont know whats going on – Julius Dec 02 '15 at 06:32
  • There is no difference. It is lamdba expression. You can even change it to x as in this case
    @Html.EditorFor(x => x.Title)
    – Julius Depulla Dec 02 '15 at 06:59
  • The only time you need the model is @Model.SomeProperty; That has to always be @Model – Julius Depulla Dec 02 '15 at 07:00
  • oh i understand i was wondered thanks – Julius Dec 02 '15 at 07:02
  • Where are you getting the null exception? Put a break point and step through – Julius Depulla Dec 02 '15 at 07:10
  • I notice you are missing credentials. Take a look at this http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – Julius Depulla Dec 02 '15 at 07:22
  • my credentials are in the web config, the error is the message.body, the email goes through and when i get an email its like this "Email From: () Message:" its blank the parameters did not fill message and email from i cant string together the body using the parameters, the parameters are null thats why it not showing in the email, but i filled the fields before pressing submit so im missing a connection between my html.editorfor and my action "Contact" – Julius Dec 02 '15 at 07:37

1 Answers1

0

Controller:

  1. In the past I've seen controller actions return ActionResult's but never a async Task<ActionResult> never mind

  2. MVC sometimes doesn't like the names of things, I would trying naming your Client model that you pass in, client.

HTML:

  1. Your BeginForm() is also missing your action and controller, but I'm sure you just excluded those.

Debugging Tip:

  • Open your web browsers network view. 'Ctrl+Shift+j' in Chrome. And click your create button. When you open the packet that create sent. Do you see all the fields of your model? (ie. Client.Title = "fancy title")
DjayFresh
  • 31
  • 6
  • thank you for replying, the moment i click the button because of my try and catch this happens, Data added successfullySystem.NullReferenceException: Object reference not set to an instance of an object – Julius Dec 02 '15 at 06:37
  • i cant see what im doing wrong, visual studios is picking it up, in my client class it is even saying the intial firstname and title is being referenced @djayfresh – Julius Dec 02 '15 at 06:40