2016-09-11 1 views
0

안녕하세요 :) 경로 값에서 Umbraco 경로 정의를 찾을 수 없으며 요청이 Umbraco 요청의 맥락에서 이루어져야합니다 "라는 오류가 있습니다. "RedirectToCurrentUmbracoPage();를 반환하십시오." ajax 양식을 seding 후.Umbraco 경로 정의 -Ajax 양식

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(); 
    } 
} 

이 양식 코드 :

내 cotroller 모두의

@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 컨텍스트는이 동안 전달되거나 채워지지 않습니다. 동일한 페이지를 새로 고침/새로 고침하려는 경우 액션의 자바 스크립트 결과를 사용하고 간단한 다시로드를 수행 할 수 있습니다. 예 :

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/).

Ajax 호출이 아닌 일반적인 SurfaceController 액션 인 경우 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"); 
} 

희망이 있으면 도움이 될 것입니다.