基本的Umbraco 6.1.1 SurfaceController问题

mis*_*man 6 asp.net-mvc umbraco asp.net-mvc-4

我搜索了所有可以找到的教程,我仍然遇到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; }
}
Run Code Online (Sandbox Code Playgroud)

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");
    }

}
Run Code Online (Sandbox Code Playgroud)

Contact.cshtml:

@model ContactFormModel1

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

ContactMacroPartial.cshtml:

@inherits Umbraco.Web.Macros.PartialViewMacroPage

@Html.Action("ContactForm", "ContactSurface")
Run Code Online (Sandbox Code Playgroud)

我的问题:

  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方法.

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

mis*_*man 3

我想花点时间说说我是如何解决这个问题的。经过更多的尝试后,我意识到我并没有真正清楚地表达我的问题。基本上,我想做的就是将 MVC 表单嵌入到部分视图宏中,以便它可以在页面内容中使用(而不是嵌入到模板中)。

我可以让这个解决方案发挥作用,但我真的不喜欢作者在视图文件中放入了多少逻辑。所以我这样调整了他的解决方案:

部分视图宏 (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())
    }

}
Run Code Online (Sandbox Code Playgroud)

表单部分视图 (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 *@
}
Run Code Online (Sandbox Code Playgroud)

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
    }

}
Run Code Online (Sandbox Code Playgroud)

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

我几乎肯定有更好/更正确的方法来做到这一点,但我没有时间来完成这个项目。如果有人有更好的解决方案,请留言!

最后一个问题/注意事项:您会注意到分部视图宏将 Request.Form 发送到 ContactSurfaceController.CreateComment,并且 MVC 神奇地为我序列化它。这样很安全,是吗?如果是这样,MVC 不是很酷吗?:)