在Umbraco建立联系表格6

时间:2013-07-16 17:57:27

标签: c# .net asp.net-mvc umbraco

我正在尝试按照以下this教程在umbraco v6中构建联系表单。我遇到的问题是:a)表格似乎是在负载上张贴即。默认情况下,我在表单上显示必填字段错误; b)当我实际填充数据并提交时,我在以下位置收到404未找到错误:请求的URL:/ umbraco / RenderMvc

以下是我的模型

的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Security;
using umbraco.cms.businesslogic.member;
using System.ComponentModel.DataAnnotations;

namespace CustomControls
{
    public class ContactFormModel
    {
        [Required, Display(Name = "First name")]
        public string FirstName { get; set; }

        [Required, Display(Name = "Surname")]
        public string Surname { get; set; }

        [Required, Display(Name = "Company")]
        public string Company { get; set; }

        [Required, Display(Name = "Email address")]
        [DataType(DataType.EmailAddress)]        
        public string EmailAddress { get; set; }

        [Required, Display(Name = "Telephone")]
        public string Telephone { get; set; }
    }
}

这是我的 SurfaceController 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Security;
using umbraco.cms.businesslogic.member;
using System.ComponentModel.DataAnnotations;
namespace CustomControls
{
    public class ContactFormSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
        [HttpGet]
        [ActionName("ContactForm")]
        public ActionResult ContactFormGet(ContactFormModel model)
        {
            return PartialView("ContactForm", new ContactFormModel());            
        }
        [HttpPost]
        [ActionName("ContactForm")]
        public ActionResult ContactFormPost(ContactFormModel model)
        {    
            //model not valid, do not save, but return current umbraco page
            if (!ModelState.IsValid)
            {
                //Perhaps you might want to add a custom message to the ViewBag
                //which will be available on the View when it renders (since we're not 
                //redirecting)     
                TempData.Add("Status", "Invalid form data");
                //return CurrentUmbracoPage();
                return RedirectToCurrentUmbracoPage();
            }
            //if validation passes perform whatever logic
            //In this sample we keep it empty, but try setting a breakpoint to see what is posted here
            //Perhaps you might want to store some data in TempData which will be available 
            //in the View after the redirect below. An example might be to show a custom 'submit
            //successful' message on the View, for example:
            TempData.Add("Status", "Your form was successfully submitted at " + DateTime.Now);
            //redirect to current page to clear the form
            return RedirectToCurrentUmbracoPage();

            //Or redirect to specific page
            //return RedirectToUmbracoPage(1063);
        }
    }
}

最后这是我的视图

@model CustomControls.ContactFormModel

@using(Html.BeginUmbracoForm("SubmitContactForm", "ContactFormSurface"))
{
    @Html.EditorFor(x => Model)
    <input type="submit"/>
}

<p class="field-validation-error">@TempData["Status"]</p>

这是我在模板中用来呈现表单的代码片段:

@Html.Action("ContactForm","ContactFormSurface")

有人能指出我出错的地方吗?

由于

3 个答案:

答案 0 :(得分:1)

在这种情况下,不要将其渲染为子动作:

@Html.Action("ContactForm","ContactFormSurface")

您应该将视图渲染为部分视图:

@Html.Partial("ContactFormSurface/ContactForm")

...指向视图所在的位置。

答案 1 :(得分:1)

啊哈,我发现了这个问题。在我看来,我正在使用这一行:

@using(Html.BeginUmbracoForm("SubmitContactForm", "ContactFormSurface"))

然而“SubmitContactForm”不存在 - 当我更新我的方法名称时,我忘了更新名称: - )

答案 2 :(得分:0)

您是否尝试将部分内容移至〜/ Views / Shared文件夹?

至于视图中的部分渲染,这对我有用。

@Html.Action(actionName: "ContactForm", controllerName: "ContactFormSurface")