【问题标题】:dropdownlistfor produce system.nullreferenceExceptiondropdownlistfor 产生 system.nullreferenceException
【发布时间】: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


    【解决方案1】:

    您正在将下拉列表与m=&gt;m.carrier 绑定,为什么您又提到了Model.Carrier不需要,删除它。

    并使用这样的东西:

    @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"));
    

    希望这会有所帮助...

    【讨论】:

    • 非常感谢!事情就是这样发生的。
    【解决方案2】:

    看起来您正在使用 Model.Carrier 作为 DropDownListFor. 中 SelectedValue 参数的值

    要么删除Model.Carrier,要么在其中输入一个值。

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多