gpt4 book ai didi

asp.net-mvc - Umbraco 6.1.1 SurfaceController 基本问题

转载 作者:行者123 更新时间:2023-12-02 12:12:45 25 4
gpt4 key购买 nike

我已经搜索了所有能找到的可用教程,但在使用 Umbraco 表面 Controller 时仍然遇到问题。我创建了一个简单的 Surface Controller 示例,它可以工作,但存在一些问题。到目前为止,这是我的代码,需要遵循的问题:

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 方法,但我尝试过的其他所有方法都会引发错误。

    当我尝试返回 RedirectToCurrentUmbracoPage() 时,我得到Cannot
    在路由值中找到 Umbraco 路由定义,请求
    必须在 Umbraco 请求的上下文中进行

    当我尝试返回 CurrentUmbracoPage() 时,我得到只能使用
    使用时在 Http POST 上下文中的 UmbracoPageResult
    SurfaceController 表单

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

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

我确信我错过了一些非常愚蠢的事情,但我一生都无法弄清楚这一点。

最佳答案

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

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

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

}

表单部分 View (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
}

}

Controller 的设计使得表单可以嵌入到模板或部分 View 宏中。如果它嵌入在模板中,则该表单应发布到 ubCreateComment ;如果它在宏中,请发布到 CreateComment .

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

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

关于asp.net-mvc - Umbraco 6.1.1 SurfaceController 基本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17532639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com