返回具有非初始化模型的视图时出现NullReferenceException

时间:2017-01-26 12:33:44

标签: c# asp.net-mvc

我在编写ASP.NET MVC网站。

我有表格,我需要发送电子邮件。

这是我的模特

 public class IndexViewModel
{
    public bool HasPassword { get; set; }
    public IList<UserLoginInfo> Logins { get; set; }
    public string PhoneNumber { get; set; }
    public bool TwoFactor { get; set; }
    public bool BrowserRemembered { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

这是视图

  @model SmartSolutions.Models.IndexViewModel

@{
    ViewBag.Title = "Админ панель";
}

<h2>@ViewBag.Title.</h2>

<p class="text-success">@ViewBag.StatusMessage</p>
<div>
    <h4>Управление</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>Пароль:</dt>
        <dd>
            [
            @if (Model.HasPassword)
            {
                @Html.ActionLink("Сменить пароль", "ChangePassword")
            }
            else
            {
                @Html.ActionLink("Создать", "SetPassword")
            }
            ]
        </dd>
        <dt>Внешний логин:</dt>
        <dd>
            @Model.Logins.Count [
            @Html.ActionLink("Управление", "ManageLogins") ]
        </dd>
        <dt>Phone Number:</dt>
        <dd>
            @(Model.PhoneNumber ?? "None") [
            @if (Model.PhoneNumber != null)
            {
                @Html.ActionLink("Change", "AddPhoneNumber")
                @: &nbsp;|&nbsp;
                @Html.ActionLink("Remove", "RemovePhoneNumber")
            }
            else
            {
                @Html.ActionLink("Add", "AddPhoneNumber")
            }
            ]
        </dd>
        <dt>Two-Factor Authentication:</dt>
        <dd>
            @if (Model.TwoFactor)
            {
                using (Html.BeginForm("DisableTwoFactorAuthentication", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                {
                    @Html.AntiForgeryToken()
                    <text>Enabled
                        <input type="submit" value="Disable" class="btn btn-link" />
                    </text>
                }
            }
            else
            {
                using (Html.BeginForm("EnableTwoFactorAuthentication", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                {
                    @Html.AntiForgeryToken()
                    <text>Disabled
                        <input type="submit" value="Enable" class="btn btn-link" />
                    </text>
                }
            }
        </dd>
        @*
            Phone Numbers can used as a second factor of verification in a two-factor authentication system.

             See <a href="http://go.microsoft.com/fwlink/?LinkId=403804">this article</a>
                for details on setting up this ASP.NET application to support two-factor authentication using SMS.

             Uncomment the following block after you have set up two-factor authentication
        *@
        @*  
            <dt>Phone Number:</dt>
            <dd>
                @(Model.PhoneNumber ?? "None")
                @if (Model.PhoneNumber != null)
                {
                    <br />
                    <text>[&nbsp;&nbsp;@Html.ActionLink("Change", "AddPhoneNumber")&nbsp;&nbsp;]</text>
                    using (Html.BeginForm("RemovePhoneNumber", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                    {
                        @Html.AntiForgeryToken()
                        <text>[<input type="submit" value="Remove" class="btn-link" />]</text>
                    }
                }
                else
                {
                    <text>[&nbsp;&nbsp;@Html.ActionLink("Add", "AddPhoneNumber")
                }
            </dd>
        *@
        <dt>Two-Factor Authentication:</dt>
        <dd>
            <p>
                There are no two-factor authentication providers configured. See <a href="http://go.microsoft.com/fwlink/?LinkId=403804">this article</a>
                for details on setting up this ASP.NET application to support two-factor authentication.
            </p>
            @*@if (Model.TwoFactor)
                {
                    using (Html.BeginForm("DisableTwoFactorAuthentication", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                    {
                        @Html.AntiForgeryToken()
                        <text>Enabled
                        <input type="submit" value="Disable" class="btn btn-link" />
                        </text>
                    }
                }
                else
                {
                    using (Html.BeginForm("EnableTwoFactorAuthentication", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                    {
                        @Html.AntiForgeryToken()
                        <text>Disabled
                        <input type="submit" value="Enable" class="btn btn-link" />
                        </text>
                    }
                }*@
        </dd>
    </dl>
    <div>
        <fieldset>
            <legend>
                Отправить приглашение
            </legend>
            @using (Html.BeginForm())
            {
                @Html.ValidationSummary()
                <p>From: </p>
                <p>@Html.TextBoxFor(m => m.From)</p>
                <p>To: </p>
                <p>@Html.TextBoxFor(m => m.To)</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>
    </div>
</div>

Controller中有一些代码。

 //
    // GET: /SendMailer/  

    [HttpPost]
    public ActionResult Index(IndexViewModel _objModelMail)
    {
        if (ModelState.IsValid)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(_objModelMail.To);
            mail.From = new MailAddress(_objModelMail.From);
            mail.Subject = _objModelMail.Subject;
            string Body = _objModelMail.Body;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("suhomlin.eugene93@gmail.com", "dontoretto23");// Enter seders User name and password
            smtp.EnableSsl = true;
            smtp.Send(mail);
            return View("Index",_objModelMail);
        }
        else
        {
            return View();
        }
    }

当我运行应用程序时,我可以发送电子邮件,我会在电子邮箱中看到它。所以它发送,但在此之后我有了这个

error

我如何解决这个问题?

感谢您的帮助。

更新

选择继续后,我有了这个。

enter image description here

0 个答案:

没有答案