2

我正在尝试按照以下教程在 umbraco v6 中构建联系表单。我遇到的问题是 a) 表单似乎是在加载时发布的,即。默认情况下,我会在表单上看到必填字段错误,并且 b) 当我实际填充数据并提交时,我在以下位置收到 404 not found 错误:请求的 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")

有人能指出我哪里出错了吗?

谢谢

4

3 回答 3

1

在这种情况下,而不是呈现为子操作:

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

您应该将视图呈现为局部视图:

@Html.Partial("ContactFormSurface/ContactForm")

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

于 2013-07-17T09:10:08.413 回答
1

啊哈,我发现了问题。在我看来,我正在使用这条线:

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

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

于 2013-07-17T10:16:45.140 回答
0

您是否尝试将部分移动到 ~/Views/Shared 文件夹?

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

@Html.Action(actionName: "ContactForm", controllerName: "ContactFormSurface")
于 2013-09-27T08:03:23.807 回答