So I was able to get this code to work when I had Email setup as its own view. But when I changed it to a form in the footer it wont work. whenever I click submit it just reloads the main index page. I have the form written out in its own view and then a renderpage in the footer section of the _layout.cshtml page. Any help in figuring this out would be wonderful as I have been trying to figure it out for a couple days now.

HomeController:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; // to give this controller access to the folders models using ToDoListMaster.Models; using System.Net; using System.Net.Mail; using System.Threading.Tasks;

namespace ToDoListMaster.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public ActionResult Purpose()
    {
        ViewBag.Message = "Your purpose page.";

        return View();
    }


    public ActionResult Email()
    {
        return View();
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> _footer(user newUser)
    {
        if(ModelState.IsValid)
        {
            var body = "<p>Email From: {0} {1}</p><p>Message:</p><p>{2}</p>";
            var Message = new MailMessage();
            Message.To.Add(new MailAddress("todolistapptest@gmail.com"));
            Message.From = new MailAddress(newUser.email);
            Message.Subject = newUser.subject;
            Message.Body = string.Format(body, newUser.firstName, newUser.lastName, newUser.message);
            Message.IsBodyHtml = true;

            using (SmtpClient smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "*****",  
                    Password = "*****"  
                };
                smtp.Credentials = credential;       
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                await smtp.SendMailAsync(Message); 
                return RedirectToAction("Sent");
            }
        }
        return View("_footer", newUser);
    }

public ActionResult Sent() { return View(); } } }

_footer:

@model ToDoListMaster.Models.user

@{ ViewBag.Title = "_footer"; }

Got something to say? Say it!

@using (Html.BeginForm(new { @class = "emailForm" })) { @Html.AntiForgeryToken()
@Html.LabelFor(m => m.firstName, new { @class = "control-Label" }) @Html.TextBoxFor(m => m.firstName, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.firstName) @Html.LabelFor(m => m.lastName, new { @class = "control-label" }) @Html.TextBoxFor(m => m.lastName, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.lastName) @Html.LabelFor(m => m.phone, new { @class = "control-label" }) @Html.TextBoxFor(m => m.phone, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.phone) @Html.LabelFor(m => m.email, new { @class = "control-label" }) @Html.TextBoxFor(m => m.email, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.email) @Html.LabelFor(m => m.subject, new { @class = "control-label" }) @Html.TextAreaFor(m => m.subject, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.subject) @Html.LabelFor(m => m.message, new { @class = "control-label" }) @Html.TextAreaFor(m => m.message, new { @class = "form-control", @cols = 40, @rows = 10 }) @Html.ValidationMessageFor(m => m.message) }

Follow Me! Im Awesome!


and something else here

© @DateTime.Now.Year - My ASP.NET Application

@Scripts.Render("~/bundles/jqueryval")

Model:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations;

namespace ToDoListMaster.Models { public class user { [Required, Display(Name ="First Name:")] public string firstName { get; set; }

    [Required, Display(Name = "Last Name:")]
    public string lastName { get; set; }

    [Required, Display(Name = "Phone Number:")]
    [Phone]
    public string phone { get; set; }

    [Required, Display(Name = "Email Address:")]
    [EmailAddress]
    public string email { get; set; }

    [Required, Display(Name = "Subject")]
    public string subject { get; set; }

    [Required, Display(Name = "Comments")]
    [UIHint("MultilineText")]
    public string message { get; set; }
} }

Related posts

Recent Viewed