因此,当我将电子邮件设置作为自己的视图时,我能够使用此代码。但是,当我将其更改为页脚中的表单时,它将无法正常工作。每当我点击提交它时,只需重新加载主索引页面。我有自己的视图中写出的表单,然后是_layout.cshtml页面的页脚部分中的一个渲染页面。任何有关解决这个问题的帮助都会很精彩,因为我现在已经想了几天。
HomeController中:
使用System;使用System.Collections.Generic;使用System.Linq; 使用System.Web;使用System.Web.Mvc; //给这个控制器 使用ToDoListMaster.Models访问文件夹模型;运用 System.Net;使用System.Net.Mail;使用System.Threading.Tasks;
命名空间ToDoListMaster.Controllers { 公共类HomeController:控制器 { 公共ActionResult索引() { 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”; }
有话要说吗?说出来!
@using(Html.BeginForm(new {@class =“emailForm”})) { @ Html.AntiForgeryToken()
@ Html.LabelFor(m =&gt; m.firstName,new {@class =“control-Label”}) @ Html.TextBoxFor(m =&gt; m.firstName,new {@class =“form-control”}) @ Html.ValidationMessageFor(m =&gt; m.firstName) @ Html.LabelFor(m =&gt; m.lastName,new {@class =“control-label”}) @ Html.TextBoxFor(m =&gt; m.lastName,new {@class =“form-control”}) @ Html.ValidationMessageFor(m =&gt; m.lastName) @ Html.LabelFor(m =&gt; m.phone,new {@class =“control-label”}) @ Html.TextBoxFor(m =&gt; m.phone,new {@class =“form-control”}) @ Html.ValidationMessageFor(m =&gt; m.phone) @ Html.LabelFor(m =&gt; m.email,new {@class =“control-label”}) @ Html.TextBoxFor(m =&gt; m.email,new {@class =“form-control”}) @ Html.ValidationMessageFor(m =&gt; m.email) @ Html.LabelFor(m =&gt; m.subject,new {@class =“control-label”}) @ Html.TextAreaFor(m =&gt; m.subject,new {@class =“form-control”}) @ Html.ValidationMessageFor(m =&gt; m.subject) @ Html.LabelFor(m =&gt; m.message,new {@class =“control-label”}) @ Html.TextAreaFor(m =&gt; m.message,new {@class =“form-control”,@ cols = 40,@rows = 10}) @ Html.ValidationMessageFor(m =&gt; m.message) }跟我来!我真棒!
以及此处的其他内容
©@ DateTime.Now.Year - 我的ASP.NET应用程序
@ Scripts.Render(“〜/ bundles / jqueryval”)
型号:
使用System;使用System.Collections.Generic;使用System.Linq; 使用System.Web;使用System.ComponentModel.DataAnnotations;
命名空间ToDoListMaster.Models { 公共类用户 { [必填,显示(姓名=“名字:”)] public string firstName {get;组; }
[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; } } }
答案 0 :(得分:0)
在您的情况下,由于您将表单移动到部分页面,因此它尝试将表单发布到由其呈现的主视图中。因此,您必须使用@ Html.BeginForm方法的操作名称和控制器名称重载,以将其指向您要发布到的正确操作方法。
简而言之,
@Html.BeginForm translates to <form method='post' action='whichever action rendered it'></form>
@using (Html.BeginForm("_footer", "Home")) transltes to <form method='post' action='home/_footer'></form>
您可以随时使用chrome inspect检查渲染的html视图,以查看其转换的确切内容。