Umbraco 路由定义-ajax 形式 [英] Umbraco route definition-ajax form

查看:23
本文介绍了Umbraco 路由定义-ajax 形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:) 我遇到错误在路由值中找不到 Umbraco 路由定义,请求必须在 Umbraco 请求的上下文中进行"并返回这个错误的代码和平 - 返回 RedirectToCurrentUmbracoPage();".seding ajax 后的形式.

Hi :) I've got error "Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request" and return this wrong peace of code - "return RedirectToCurrentUmbracoPage();". After seding ajax form.

这是我的控制器:

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 上下文.如果我们想重新加载/刷新同一页面,我们可以使用来自 action 的 Javascript 结果并执行简单的重新加载,例如

first of all, we need to understand what we want to achieve during this submission. Ajax request is not a part of typical page request and Umbraco Context is not passed / populated during this one. If we want to reload / refresh the same page, we can use the Javascript result from action and perform simple reload e.g.

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

如果我们想使用 Umbraco 上下文(Url 或其他任何东西),我们可以手动启动 UmbracoHelper 并使用它做任何我们想做的事情:

If we want to play with the Umbraco Context (Url or anything else), we can initiate UmbracoHelper manually and do whatever we want to do with it:

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

或者只是使用带有 Umbraco 实现/包装器的 WebAPI 控制器,您可以在此处阅读更多信息:https://our.umbraco.org/documentation/reference/routing/webapi/.

or just use a WebAPI controllers with Umbraco implementation / wrapper, about which you can read more here: https://our.umbraco.org/documentation/reference/routing/webapi/.

如果它是典型的 SurfaceController 操作(不是 Ajax 调用),我们可以使用 Umbraco 包装器中可用的所有可能的重定向和方法(检查:https://our.umbraco.org/documentation/reference/templating/mvc/forms).

If it would be a typical SurfaceController action (not Ajax call), we can use all possible redirects and methods available in Umbraco wrapper (check: https://our.umbraco.org/documentation/reference/templating/mvc/forms).

如果在积极提交表单后需要,我们还可以返回任何 PartialView、Content 甚至 JSON.在我看来,这是这种情况下的最佳解决方案.在你的情况下:

We can also return any PartialView, Content or even JSON if it's required after the positive submission of the form. And this is in my opinion the best solution in this case. In your case:

[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");
}

希望能帮到你!

这篇关于Umbraco 路由定义-ajax 形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆