加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – Basic Umbraco 6.1.1 SurfaceController问题

发布时间:2020-12-16 03:54:57 所属栏目:asp.Net 来源:网络整理
导读:我搜索了所有可以找到的教程,我仍然遇到Umbraco Surface控制器的问题.我已经创建了一个简单的表面控制器示例,该示例有效,但有一些问题.这是我的代码到目前为止,要遵循的问题: ContactformModel1.cs: public class ContactFormModel1{ public string Email
我搜索了所有可以找到的教程,我仍然遇到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")

我的问题:

>我很确定返回ContactFormGet(模型)是错误的
ContactFormPost方法,但我尝试过的其他所有方法都会抛出错误.

当我尝试返回RedirectToCurrentUmbracoPage()时,我得到了不能
在路由值中找到Umbraco路由定义,即请求
必须在Umbraco请求的背景下进行.

当我尝试返回CurrentUmbracoPage()时,我得到的只能使用
UmbracoPageResult在Http POST的上下文中使用时
SurfaceController表单.
>路由似乎正常工作(当我在ContactFormPost中放置断点时,调试器停在那里).但是当表单返回时,我会得到我提交的确切值.我没看到!!!附加到电子邮件地址. (注意,这段代码只是用于调试,它并不意味着做任何有用的事情).
>如何在控制器中调用“SayOK”方法?当我将BeginUmbracoForm方法更改为指向SayOK时,我仍然陷入了ContactFormPost方法.

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

解决方法

我想花点时间说出我是如何解决这个问题的.在玩了一些之后,我意识到我并没有清楚地说出我的问题.基本上,我要做的就是在部分视图宏中嵌入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不摇滚吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读