Domanda

I'm trying to create a webpage to send text msg to phone. I'm having trouble getting the carrier drop down list right. It always tells me that "an exception of type 'System.NullReferenceException' occurred in App_Web_ucvryg25.dll but was not handled in user code" when I try to run the code.

Here is my view:

    @model MVCTesting2.Models.MailModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<fieldset>
<legend>
    Send Text
</legend>
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <p>Phone number: </p>
    <p>@Html.TextBoxFor(m => m.To)</p>
    <p>Carriers</p>
    <p>@Html.DropDownListFor(m => m.Carrier,
                                 new SelectList(
                new List<Object>{ 
                   new { value = "@text.att.net" , text = "att"  },
                   new { value = "@cingularme.com" , text = "cingular" },
                   new { value = "@messaging.nextel.com" , text = "nextel"}
                },
                "value",
                "text",
                 Model.Carrier))</p>

    <p>Subject: </p>
    <p>@Html.TextBoxFor(m => m.Subject)</p>
    <p>Body: </p>
    <p>@Html.TextAreaFor(m => m.Body)</p>
    <input type="submit" value="Send" />
}
</fieldset>

Here is my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc; 

namespace MVCTesting2.Controllers
{
public class SendTextController : Controller
{
    //
    // GET: /SendText/
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ViewResult Index(MVCTesting2.Models.MailModel _objModelMail)
    {
        if (ModelState.IsValid)
        {
            string from = "myemail";

            string to = _objModelMail.To;

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from, _objModelMail.From, System.Text.Encoding.UTF8);
            mail.Subject = _objModelMail.Subject;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = _objModelMail.Body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            //Add the Creddentials- use your own email id and password

            client.Credentials = new System.Net.NetworkCredential(from, "mypassword");

            client.Port = 587; // Gmail works on this port<o:p />
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            client.Send(mail);

            return View("Send");
        }
        else
        {
            return View();
        }
    }
}
}

The model is pretty simple with all the variables.

Any idea why this happened?

Thanks in advance!

È stato utile?

Soluzione

You are binding the dropdown list with m=>m.carrier why again you are mentioning Model.Carrier. It is not required , remove it.

And use some thing like this:

@Html.DropDownListFor(m => m.Carrier,
                                 new SelectList(
                new List<Object>{ 
                   new { value = "@text.att.net" , text = "att"  },
                   new { value = "@cingularme.com" , text = "cingular" },
                   new { value = "@messaging.nextel.com" , text = "nextel"}
                },
                "value",
                "text"));

Hope this helps...

Altri suggerimenti

Looks like you're using Model.Carrier as value for the SelectedValue parameter in the DropDownListFor.

Either you remove Model.Carrier or you put a value in it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top