我已經使用控制器,視圖和viewmodel在ASP.NET中做了一個簡單的聯繫表單。在ASP.NET中的聯繫人窗體只有在POST請求刷新頁面
該表格是在Umbraco中製作的,因此語法可能略有不同。我正在使用BeginUmbracoForm幫助器方法來發出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("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
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>
}
}