0

I writing ASP.NET MVC website.

I have form where I need to send email.

Here is my model

 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; }
}

Here is view

  @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>

And there is some code in 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();
        }
    }

When I run app I can send email and I see it on my email box. So it sends, but after this I have this

error

How I can fix this?

Thank's for help.

UPDATE

After select Continue I have this.

enter image description here

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Basically in your controller when model is not valid you return view without passing the model. Since model is null in your view you get an error on model dot something. If you're not sure why model isn't valid then check this http://stackoverflow.com/questions/1791570/modelstate-isvalid-false-why – derloopkat Jan 26 '17 at 13:07
  • Thank's , I solved problem just create new model, controller and view @derloopkat –  Jan 26 '17 at 14:36

0 Answers0