Umbraco路线定义-ajax形式

时间:2016-09-11 12:41:22

标签: c# .net ajax umbraco umbraco7

嗨:)我有错误“无法在路由值中找到Umbraco路由定义,请求必须在Umbraco请求的上下文中进行”并返回错误的代码 - “返回RedirectToCurrentUmbracoPage(); ”。在找到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("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">
}

1 个答案:

答案 0 :(得分:1)

首先,我们需要了解在提交过程中我们想要实现的目标。 Ajax请求不是典型页面请求的一部分,并且在此期间不传递/填充Umbraco Context。如果我们想重新加载/刷新同一页面,我们可以使用来自操作的Javascript结果并执行简单的重新加载,例如。

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

如果我们想玩Umbraco Context(Url或其他任何东西),我们可以手动启动UmbracoHelper并做任何我们想做的事情:

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

或者只使用带有Umbraco实现/包装器的WebAPI控制器,您可以在这里阅读更多内容: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("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");
}

希望它能帮到你!