【发布时间】:2018-02-10 05:23:23
【问题描述】:
我使用控制器、视图和视图模型在 ASP.NET 中制作了一个简单的联系表单。
表格是用 Umbraco 制作的,因此语法可能看起来略有不同。我正在使用 BeginUmbracoForm 辅助方法发出 POST 请求并在控制器中调用 post 操作。
但是当我点击提交按钮时,页面被刷新,没有发布动作发生。此时无法发送邮件,因为我正在从本地服务器运行此邮件,但我希望显示 FormSubmitted 消息,以表明一切正常。
为什么没有发出 POST 请求?
这是我的控制器,它继承自 SurfaceController。
public class ContactController : SurfaceController
{
public ActionResult Index()
{
return PartialView("ContactForm", new ContactFormViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit(ContactFormViewModel model)
{
if (!ModelState.IsValid) //validation
return CurrentUmbracoPage();
// Send mail using SMTP
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("thomasbrun12@gmail.com");
mailMessage.From = new MailAddress("info@remaloevbjerg.dk");
mailMessage.Subject = "Kontakt mail";
mailMessage.Body = "Navn: " + model.Navn + "\nAddresse: " + model.Addresse + ", " +
model.PostNrBy + "\nTelefon: " + model.Telefon + "\n" + model.Message;
SmtpClient smtpClient = new SmtpClient("smtp.curanet.dk");
smtpClient.Send(mailMessage);
TempData["FormSubmitted"] = true;
return RedirectToCurrentUmbracoPage();
}
}
这是我的观点:
@model LoevbjergRema1000Umbraco.Models.ContactFormViewModel
@using LoevbjergRema1000Umbraco.Controllers;
@{
Html.EnableClientValidation(true);
Html.EnableUnobtrusiveJavaScript(true);
}
@if (Convert.ToBoolean(TempData["FormSubmitted"]))
{
<div>Tak for din henvendelse.</div>
}
else
{
using (Html.BeginUmbracoForm<ContactController>("Submit"))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
<div class="col-sm-8">
<div class="row">
<div class="col-sm-4">
<form class="form-inline" id="kontaktForm">
<div class="form-group">
<label for="">Navn</label>
@Html.TextBoxFor(m => m.Navn, new { @class = "form-control", @placeholder = "Indtast navn..." })
@Html.ValidationMessageFor(m => m.Navn)
<label for="">Addresse</label>
@Html.TextBoxFor(m => m.Addresse, new { @class = "form-control", @placeholder = "Indtast addresse..." })
<label for="">Postnr. + by</label>
@Html.TextBoxFor(m => m.PostNrBy, new { @class = "form-control", @placeholder = "Indtast postnr & by..." })
<label for="">Telefon</label>
@Html.TextBoxFor(m => m.Telefon, new { @class = "form-control", @placeholder = "Indtast telefonnr..." })
<label for="">Email</label>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Indtast email..." })
@Html.ValidationMessageFor(m => m.Email)
</div>
</form>
</div>
<div class="col-sm-8">
<label for="">Skriv din besked</label>
@Html.TextAreaFor(m => m.Message, new { @class = "form-control", @id = "kontaktBesked", @rows = "11", @placeholder = "Indtast din besked her..." })
@Html.ValidationMessageFor(m => m.Message)
<button class="btn_1 pull-right contact-btn" name="submit" type="submit">Afsend Besked</button>
<button class="btn_1 contact-btn">Ryd felter</button>
</div>
</div>
</div>
}
}
【问题讨论】:
标签: c# asp.net asp.net-mvc forms post