2017-09-01 15 views
0

我已經使用控制器,視圖和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> 
    } 
} 

回答

0

如果你覺得你的表格是不是正確張貼然後大概有幾件事情需要檢查,

是你的模型是否有效?是否有某種正在嘗試着火和失敗的驗證事件,請查看您的控制檯並查看是否出現某種錯誤。這可能會導致瀏覽器刷新而不是提交表單。

如果get正在發生的事情,而不是post,你可以強迫你的表格是一個後在窗體構造函數(Html.BeginUmbracoForm)指定FormMethod.Post,你不應該真的要做到這一點,看看在生成的html是表單標籤正確生成(因爲它的umbraco它發佈的url不會像標準MVC那樣直接,因爲它是由Html.BeginUmbracoForm方法生成的,但它應該仍然有意義)