2016-09-11 9 views
0

こんにちは:)私は「Umbracoのリクエストの文脈でUmbracoルート定義を見つけることができません」というエラーがあり、 "return RedirectToCurrentUmbracoPage();" ajaxフォームを退治した後。Umbracoルート定義-Ajaxフォーム

これは私のcotrollerです:

public class ContactController : Umbraco.Web.Mvc.SurfaceController 
{ 
    private string GMAIL_SERVER = "smtp.gmail.com"; 
    private int PORT = 587; 

    [ChildActionOnly] 
    public ActionResult ContactForm() 
    { 
     var model = new ContactFormModel() 
     { 
      Email = "", 
      Name = "", 
      Subject = "", 
      Message = "" 
     }; 

     return PartialView("ContactForm", model); 
    } 

    [NotChildAction] 
    [HttpPost] 
    public ActionResult ContactForm(ContactFormModel model) 
    { 
     var fromAddress = new MailAddress("[email protected]", model.Name); 
     var toAddress = new MailAddress("[email protected]", "To Name"); 
     string fromPassword = "xxx"; 
     string subject = model.Subject; 
     string body = model.Message; 

     var smtp = new SmtpClient 
     { 
      Host = "smtp.gmail.com", 
      Port = 587, 
      EnableSsl = true, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      Credentials = new NetworkCredential(fromAddress.Address, fromPassword), 
      Timeout = 20000 
     }; 

     if (!ModelState.IsValid) 
     {   
      return CurrentUmbracoPage(); 
     } 

     var message = new MailMessage(fromAddress, toAddress) 
     { 
      Subject = model.Subject, 
      Body = "test" 
     }; 
     smtp.Send(message); 

     return RedirectToCurrentUmbracoPage(); 
    } 
} 

と、このフォームコード:すべての

@using (Ajax.BeginForm("ContactForm" ,"Contact", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace })) 
{ 
    @Html.TextBoxFor(m => m.Name, null, new { name = "name", id = "name", placeholder = "Name" }) 
    @Html.ValidationMessageFor(m => m.Name) 
    @Html.TextBoxFor(m => m.Email, null, new { name = "email", id = "email", placeholder = "Email address" }) 
    @Html.ValidationMessageFor(m => m.Email) 
    @Html.TextBoxFor(m => m.Subject, null, new { name = "subject", id = "subject", placeholder = "Subject" }) 
    @Html.ValidationMessageFor(m => m.Subject) 
    @Html.TextAreaFor(m => m.Message, new { rows = "", cols = "", name = "message", id = "message", placeholder = "Your message" }) 
    @Html.ValidationMessageFor(m => m.Message) 
    <input type="submit" id="contact-submit" value="SEND MESSAGE"> 
} 

答えて

1

まず、私たちは、この提出の際に達成したいかを理解する必要があります。 Ajaxリクエストは典型的なページ要求の一部ではなく、Umbraco Contextはこの間に渡されたり、取り込まれたりしません。同じページを再読み込み/更新したい場合は、アクションのJavascriptの結果を使用して簡単なリロードを実行できます。

return JavaScript("location.reload(true)"); 

我々はUmbracoコンテキスト(URLまたは他の何か)で再生したい場合は、我々は手動でUmbracoHelperを開始し、我々はそれでやりたいものは何でも行うことができます。

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); 
var node = umbracoHelper.TypedContent(nodeId); 
//... 

またはちょうどWebAPIのを使用Umbracoの実装/ラッパーを備えたコントローラについては、ここで詳しく読むことができます:https://our.umbraco.org/documentation/reference/routing/webapi/

典型的なSurfaceControllerアクション(Ajax呼び出しではない)の場合、Umbracoラッパーで利用可能なすべてのリダイレクトとメソッドを使用できます(チェック:https://our.umbraco.org/documentation/reference/templating/mvc/forms)。

フォームの肯定的な投稿後に必要な場合は、PartialView、コンテンツ、またはJSONも返すことができます。そして、これは私の意見ではこの場合の最善の解決策です。あなたのケースでは:

[NotChildAction] 
[HttpPost] 
public ActionResult ContactForm(ContactFormModel model) 
{ 
    var fromAddress = new MailAddress("[email protected]", model.Name); 
    var toAddress = new MailAddress("[email protected]", "To Name"); 
    string fromPassword = "xxx"; 
    string subject = model.Subject; 
    string body = model.Message; 

    var smtp = new SmtpClient 
    { 
     Host = "smtp.gmail.com", 
     Port = 587, 
     EnableSsl = true, 
     DeliveryMethod = SmtpDeliveryMethod.Network, 
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword), 
     Timeout = 20000 
    }; 

    if (!ModelState.IsValid) 
    {   
     return PartialView("_Error"); 
    } 

    var message = new MailMessage(fromAddress, toAddress) 
    { 
     Subject = model.Subject, 
     Body = "test" 
    }; 
    smtp.Send(message); 

    return PartialView("_Success"); 
} 

は、それはあなたを助けることを願っています!