【发布时间】:2014-03-19 14:41:33
【问题描述】:
我正在尝试创建一个网页来向手机发送短信。我无法正确获取运营商下拉列表。当我尝试运行代码时,它总是告诉我“App_Web_ucvryg25.dll 中发生了‘System.NullReferenceException’类型的异常,但未在用户代码中处理”。
这是我的看法:
@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>
这是我的控制器:
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();
}
}
}
}
模型非常简单,包含所有变量。
知道为什么会这样吗?
提前致谢!
【问题讨论】:
标签: asp.net-mvc html.dropdownlistfor