[英]Umbraco route definition-ajax form
嗨:)我遇到了錯誤“無法在路由值中找到Umbraco路由定義,該請求必須在Umbraco請求的上下文中進行”,並返回此錯誤的代碼–“ return RedirectToCurrentUmbracoPage();”。 鎮靜后再用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("xxx@gmail.com", model.Name);
var toAddress = new MailAddress("xxx@gmail.com", "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">
}
首先,我們需要了解在提交過程中要達到的目標。 Ajax請求不是典型頁面請求的一部分,並且在此過程中未傳遞/填充Umbraco上下文。 如果我們想重新加載/刷新同一頁面,我們可以使用動作的Javascript結果並執行簡單的重新加載,例如
return JavaScript("location.reload(true)");
如果我們想使用Umbraco Context(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,Content或JSON。 我認為這是這種情況下的最佳解決方案。 在您的情況下:
[NotChildAction]
[HttpPost]
public ActionResult ContactForm(ContactFormModel model)
{
var fromAddress = new MailAddress("xxx@gmail.com", model.Name);
var toAddress = new MailAddress("xxx@gmail.com", "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");
}
希望對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.