在路线值中找不到Umbraco路线

时间:2017-09-21 22:16:30

标签: asp.net razor umbraco

我花了很多时间用Umbraco建立一个网站,我几乎已经完成了,除非我无法通过HttpPost获取我的联系人视图。因为我收到以下错误: enter image description here

我已经尝试了很多我在这里和Umbraco论坛上找到的东西但到目前为止没有任何用处,起初我认为这是验证本身所以我改变了它甚至改变了AJAX动作但是没有工作,调试后我发现了这个错误,但我不知道如何处理解决方案。

这是整个控制器代码:

  public class ContactSurfaceController : SurfaceController
    {
        public ActionResult Index()
        {
            return PartialView("ContactView", new ContactViewModel());
        }

        [HttpPost]
        [NotChildAction]
        public ActionResult Submit(ContactViewModel form)
        {

            if (!ModelState.IsValid)
            {
                return Json(new { Success = false });
            }

            SectionContact currSection = (SectionContact)Umbraco.TypedContent(form.CurrentSectionId);

            string mailTo = currSection.AdministratorsEmail;
            string mailFrom = currSection.NotificationMailFrom;
            string mailFromAlias = currSection.NotificationMailSenderAlias;
            string message = string.Concat(currSection.NotificationEmailBody, "<br/><br/>", umbraco.library.StripHtml(form.Message));

            //Use predefined subject. If predefined subject is not defined, use current subject from form (if any)
            string subject = (string.IsNullOrEmpty(currSection.NotificationEmailSubject)) ? form.Subject : currSection.NotificationEmailSubject;

            //If all else fails, use standard subject from dictionary.
            subject = (string.IsNullOrEmpty(subject)) ? Umbraco.GetDictionaryValue("ContactForm.Subject") : subject;

            StringBuilder sb = new StringBuilder(string.Empty);

            if (!string.IsNullOrEmpty(form.Name))
            {
                sb.Append("Name:");
                sb.Append("<br/>");
                sb.Append(form.Name);
                sb.Append("<br/><br/>");
            }

            if (!string.IsNullOrEmpty(form.Subject))
            {
                sb.Append("Subject:");
                sb.Append("<br/>");
                sb.Append(form.Subject);
                sb.Append("<br/><br/>");
            }

            sb.Append("Email: ");
            sb.Append("<br/>");
            sb.Append(form.Email);
            sb.Append("<br/><br/>");

            sb.Append("Message: ");
            sb.Append("<br/>");
            sb.Append(umbraco.library.ReplaceLineBreaks(message));
            sb.Append("<br/><br/>");

            if (!string.IsNullOrEmpty(currSection.Dropdown1Values))
            {
                sb.Append(currSection.Dropdown1Values.Split(new string[] { "\n", "\n\r" }, StringSplitOptions.RemoveEmptyEntries)[0]);
                sb.Append("<br/>");
                sb.Append(form.DropDown1);
                sb.Append("<br/><br/>");
            }

            if (!string.IsNullOrEmpty(currSection.Dropdown2Values))
            {
                sb.Append(currSection.Dropdown2Values.Split(new string[] { "\n", "\n\r" }, StringSplitOptions.RemoveEmptyEntries)[0]);
                sb.Append("<br/>");
                sb.Append(form.DropDown2);
                sb.Append("<br/><br/>");
            }

            if (ModelState.IsValid)
            {
                Utils.SendEmail(mailFrom, mailFromAlias, mailTo, subject, sb.ToString());
            }

            return Json(new { Success = true });
        }


    }

以下是部分视图:

@inherits Umbraco.Web.Mvc.UmbracoViewPage<SectionContact>

@using DotSee.Models;
@using DotSee.UmbracoExtensions;

@{

    SectionContact currSection = Model;
    int currSectionId = currSection.Id;

    string[] dropDown1Data = currSection.Dropdown1Values.Split(new string[] { "\n", "\n\r" }, StringSplitOptions.RemoveEmptyEntries);
    string[] dropDown2Data = currSection.Dropdown2Values.Split(new string[] { "\n", "\n\r" }, StringSplitOptions.RemoveEmptyEntries);

}

<section id="contactForm_@currSectionId" class="@currSection.GetSectionEffectCssClass() ptb-60 ptb-sm-60">
    <div class="container">

        @if (!string.IsNullOrEmpty(currSection.IntroText) || !currSection.HideTitle)
        {

            <div class="row">
                <div class="col-md-6 col-md-offset-3 text-center">

                    @if (!currSection.HideTitle)
                    {
                        <h3>@currSection.Name</h3>
                    }

                    @if (!string.IsNullOrEmpty(currSection.IntroText))
                    {
                        <p class="lead">@currSection.IntroText</p>
                    }

                </div>
            </div>
            <div class="spacer-15"></div>
        }

        @Html.Partial("ContactView", new ContactViewModel()
                   , new ViewDataDictionary
                   {
                         { "currSectionId", currSection.Id }
                       , { "hideSubject", currSection.HideSubject }
                       , { "hideName", currSection.HideName }
                       , { "dropDown1Data", dropDown1Data }
                       , { "dropDown2Data", dropDown2Data }
                   })
        </div>
</section>

最后这是视图联系人视图:

@inherits Umbraco.Web.Mvc.UmbracoViewPage<ContactViewModel>
@using DotSee.UmbracoExtensions
@using DotSee.Models
@using DotSee.Controllers
@using Umbraco.Web
@using ClientDependency.Core.Mvc;

@{
    Html.RequiresJs("/scripts/jquery.unobtrusive-ajax.min.js",9999);
    Html.RequiresJs("/scripts/jquery.validate.min.js", 9999);
    Html.RequiresJs("/scripts/jquery.validate.unobtrusive.min.js", 9999);

    int currSectionId = Convert.ToInt32(ViewData["currSectionId"]);
    bool hideSubject = (bool)ViewData["hideSubject"];
    bool hideName = (bool)ViewData["hideName"];
    string[] dropDown1Data = (ViewData["dropDown1Data"] as string[]);
    string[] dropDown2Data = (ViewData["dropDown2Data"] as string[]);
}


        <div class="row">
            <div id="contactFormMessages" class="ptb-0 col-md-6 col-md-offset-3 text-center">

                <h6 id="contactSuccess" class="successContent">
                    <i class="fa fa-check left" style="color: #5cb45d;"></i>@Umbraco.GetDictionaryValue("ContactForm.Success")
                </h6>

                <h6 class="errorContent">
                    <i class="fa fa-exclamation-circle left" style="color: #e1534f;"></i>@Umbraco.GetDictionaryValue("ContactForm.Failure")
                </h6>

               </div>

            <div id="contactFormHolder" class="col-md-6 col-md-offset-3">
                @using (Ajax.BeginForm("Submit", "ContactSurface", new AjaxOptions
                {
                    HttpMethod = "POST",
                    OnFailure = "ShowContactError",
                    OnSuccess = "ShowContactSuccess",
                }))
                {

                    @Html.TextBoxFor(model => model.CurrentSectionId, new { Value = Convert.ToInt32(currSectionId) })
                    if (!hideName)
                    {
                        <div class="form-field-wrapper">
                            @Html.TextBoxFor(model => model.Name, new { @Placeholder = "Name", @class = "input-sm form-full" })
                        </div>
                    }
                    <div class="form-field-wrapper">
                        @Html.TextBoxFor(model => model.Email, new { @Placeholder = "Email", @class = "input-sm form-full" })
                        @Html.ValidationMessageFor(model => model.Email, Umbraco.GetDictionaryValue("ContactForm.Email.Invalid"))
                    </div>
                    if (!hideSubject)
                    {
                        <div class="form-field-wrapper">
                            @Html.TextBoxFor(model => model.Subject, new { @Placeholder = "Subject", @class = "input-sm form-full" })
                        </div>
                    }
                    <div class="form-field-wrapper">
                        @Html.TextAreaFor(model => model.Message, new { @Placeholder = "Content", @class = "form-full", @rows = 7 })
                        @Html.ValidationMessageFor(model => model.Message, Umbraco.GetDictionaryValue("ContactForm.Message.Required"))
                    </div>
                    if (dropDown1Data != null && dropDown1Data.Length > 1)
                    {
                        @Html.DropDownListFor(model => model.DropDown1, dropDown1Data.Select(f => new SelectListItem { Value = f, Text = f }).Skip(1), dropDown1Data[0], new { @class = "form-full" })
                    }
                    if (dropDown2Data != null && dropDown2Data.Length > 1)
                    {
                        @Html.DropDownListFor(model => model.DropDown2, dropDown2Data.Select(f => new SelectListItem { Value = f, Text = f }).Skip(1), dropDown2Data[0], new { @class = "form-full" })
                    }
                    <button class="btn btn-md btn-black form-full" type="submit" id="form-submit" name="submit">@Umbraco.GetDictionaryValue("ContactForm.Submit")</button>
                }
            </div>
        </div>

<script type="text/javascript">

    function ShowContactError() {
         $(".errorContent").show();
            $("#contactFormHolder").hide();
    }

    function ShowContactSuccess(arg) {
        if (arg.Success === true) {
            $("#contactSuccess").show();
            $("#contactFormHolder").hide();
        }
        else
        {
            $(".successContent").show();
            $("#contactFormHolder").hide();

        }
    }

</script>

1 个答案:

答案 0 :(得分:0)

你不能在Umbraco中使用Ajax.BeginForm。您必须创建自己的Ajax表单处理程序并将其路由到〜/ Umbraco / Api / Contact / Submit

为此,您的控制器必须继承UmbracoApiController而不是SurfaceController。

如果要使用MVC而不是WebAPI,则应使用Html.BeginUmbracoForm帮助程序。在这种情况下,您的控制器应该是SurfaceController。

Umbraco WebApi