基本的Umbraco 6.1.1 SurfaceController问题

时间:2013-07-08 17:31:30

标签: asp.net-mvc asp.net-mvc-4 umbraco

我搜索了所有可以找到的教程,而且我仍然遇到了Umbraco Surface控制器的问题。我已经创建了一个简单的表面控制器示例,该示例有效,但有一些问题。到目前为止,这是我的代码,需要遵循的问题:

ContactformModel1.cs:

public class ContactFormModel1
{

    public string Email { get; set; }
    public string Name { get; set; }
    public string HoneyPot { get; set; }

    public string Title { get; set; }
    public string Last { get; set; }
    public string First { get; set; }
    public string Addr { get; set; }
    public string Phone { get; set; }
    public string Time { get; set; }
    public string Comment { get; set; }
}

ContactSurfaceController.cs:

public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController
{

    public ActionResult Index()
    {
        return Content("this is some test content...");
    }

    [HttpGet]
    [ActionName("ContactForm")]
    public ActionResult ContactFormGet(ContactFormModel1 model)
    {
        return PartialView("~/Views/ContactSurface/Contact1.cshtml", model);
    }

    [HttpPost]
    [ActionName("ContactForm")]
    public ActionResult ContactFormPost(ContactFormModel1 model)
    {
        // Return the form, just append some exclamation points to the email address
        model.Email += "!!!!";
        return ContactFormGet(model);
    }


    public ActionResult SayOK(ContactFormModel1 model)
    {
        return Content("OK");
    }

}

Contact.cshtml:

@model ContactFormModel1

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

ContactMacroPartial.cshtml:

@inherits Umbraco.Web.Macros.PartialViewMacroPage

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

我的问题:

  1. 我很确定return ContactFormGet(model)错误 ContactFormPost方法,但我尝试过的其他所有方法都会抛出错误。

    当我尝试return RedirectToCurrentUmbracoPage()时,我得到Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request

    当我尝试return CurrentUmbracoPage()时,我得到Can only use UmbracoPageResult in the context of an Http POST when using a SurfaceController form

  2. 路由似乎正常工作(当我在ContactFormPost中放置断点时,调试器停在那里)。但是当表单返回时,我会得到我提交的确切值。我没看到!!!附加到电子邮件地址。 (注意,这段代码只是用于调试,它并不意味着做任何有用的事情。)

  3. 如何在控制器中调用“SayOK”方法?当我将BeginUmbracoForm方法更改为指向SayOK时,我仍然陷入ContactFormPost方法。

  4. 我确定我错过了一些令人难以置信的愚蠢的东西,但我无法想象我的生活。

2 个答案:

答案 0 :(得分:3)

我想花点时间说出我是如何解决这个问题的。在玩了一些之后,我意识到我并没有清楚地说出我的问题。基本上,我要做的就是在部分视图宏中嵌入一个MVC表单,以便它可以用在页面的内容中(不嵌入模板中)。

我可以让this solution工作,但我真的不喜欢作者在View文件中放了多少逻辑。所以我用这种方式调整了他的解决方案:

部分视图宏(cshtml)文件:

@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Intrepiware.Models
@{
    bool isPostback = !String.IsNullOrEmpty(Request.Form["submit-button"]);
    if(isPostback)
    {
        @Html.Action("CreateComment", "ContactSurface", Request.Form)   
    }
    else
    {
        @Html.Partial("~/Views/Partials/ContactForm.cshtml", new ContactFormModel())
    }

}

表单部分视图(cshtml)文件:

@using Intrepiware.Models
@using Intrepiware.Controllers
@model ContactFormModel

<p>
    <span style="color: red;">@TempData["Errors"]</span>
</p>
<p>
    @TempData["Success"]
</p>
<div id="cp_contact_form">

@using(Html.BeginUmbracoForm("CreateComment", "BlogPostSurface"))
{
    @* Form code goes here *@
}

ContactSurfaceController.cs文件:

public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ubCreateComment(ContactFormModel model)
    {
        if (processComment(model) == false)
            return CurrentUmbracoPage();
        else
            return RedirectToCurrentUmbracoPage();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateComment(ContactFormModel model)
    {
        if(processComment(model) == true)
        {
            TempData["Success"] = "Thank you for your interest. We will be in contact with you shortly.";
            ModelState.Clear();
        }
        return PartialView("~/Views/Partials/ContactForm.cshtml");
    }

    private bool processComment(ContactFormModel model)
    {
            // Handle the model validation and processing; return true if success
    }

}

控制器的设计使得表单可以嵌入模板或部分视图宏中。如果它嵌入在模板中,则表单应发布到ubCreateComment;如果它在宏中,请发布到CreateComment

我几乎肯定有一个更好/更正确的方法,但是我没有时间去研究这个项目。如果有人有更好的解决方案,请发布!

最后一个问题/注意:您会注意到局部视图宏将Request.Form发布到ContactSurfaceController.CreateComment,而MVC为我神奇地序列化它。那是安全的,是吗?如果是这样,MVC不摇滚吗? :)

答案 1 :(得分:1)

您正在使用ChildAction,因为您指定了@Html.Action("ContactForm", "ContactSurface"),因此,在您的视图中,您需要:

  • 使用Html.BeginForm(...)而不是&#39; Html.BeginUmbracoForm(...)&#39;
  • 允许表单回发到同一路径而不是回复

如果您这样做,那么表单将按预期回发给自己。

请参阅documentation here以获取进一步的帮助。

修改

刚看到你问题的最后一部分。如果您打算SayOK成为您的“谢谢你”。消息,我只是从您的HttpPost操作中调用它,而不是返回初始视图。