【问题标题】:umbraco mvc surface controller, can't return view from HttpPost Actionumbraco mvc 表面控制器,无法从 HttpPost 操作返回视图
【发布时间】:2013-05-25 12:28:43
【问题描述】:

问题概述:

我创建了一个 Surface 控制器,其中包含使用 @Html.Action(...) 调用的操作。

@Html.Action 调用在宏局部视图中完成,宏包含在使用富文本编辑器的页面内容中。

(我是新手,所以如果我做错了事情,请告诉我。)

Surface 控制器有一个 GET 和一个 POST 操作,但它是在宏部分中调用的 get 操作。

Get action 渲染良好,在表单中不输入任何数据将使模型状态无效(这是我目前正在测试的)。

提交表单(没有输入数据)意味着我可以进入我的 POST 操作,ModelState.IsValid 设置为 false 并返回 CurrentUmbracoPage()。

一切正常...调试时没有遇到异常...

此时页面上出现错误文字“Error loading Partial View script”。

我要做的只是返回显示验证消息的同一页面。

详情:

Umbraco v6.0.5

我目前正在使用的控制器用于重置用户的密码。我还有一个登录控制器,它通过使用 RedirectToCurrentUmbracoPage() 解决了这个问题。

要访问包含宏的页面,我使用地址 http://{testhost}/Reset-Password 返回的错误文本为:加载部分视图脚本时出错(文件:~/Views/MacroPartials/ResetPassword.cshtml)

代码位于单独的解决方案中,并且视图和 bin 目录被复制。 使用了nuget包UmbracoCMS.Scaffolding。

控制器代码:

public class ResetPasswordSurfaceController : SurfaceController {        
        [ChildActionOnly]
        [HttpGet]
        public ActionResult Reset(string token, string email) {
             // Validation Code Omited             
             var user = Membership.GetUser(username);
             return PartialView("Reset", new ResetPasswordSurfaceModel { UserID =     user.ProviderUserKey.AsInt() });
        }

        [HttpPost]
        public ActionResult PostReset(ResetPasswordSurfaceModel model) {
            if (ModelState.IsValid) { 
                 //Password reset code omited                 
                  return RedirectToCurrentUmbracoPage();
             }
            //works but only partial view content is rendered
            // return PartialView("Reset",model);         
            return CurrentUmbracoPage();
        }
    }

查看 - ~\Views\ResetPasswordSurface\Reset.cshtml:

@model UmbracoExt.Models.ResetPasswordSurfaceModel
@using (Html.BeginUmbracoForm("PostReset", "ResetPasswordSurface")) {
      @Html.EditorForModel() 
    <input type="submit" value="Submit" />
}

宏局部视图 - ~\Views\MacroPartials\ResetPassword.cshtml:

@inherits Umbraco.Web.Macros.PartialViewMacroPage       
@Html.Action("Reset", "ResetPasswordSurface")

感谢任何帮助。

编辑:

从重置操作中删除 [HttpGet] 属性表明,在调用 PostReset 操作后,还会调用重置操作。

将 PostReset 重命名为 Reset 并将 httpget 属性重新添加到原始的 Reset Action 会导致 post 操作被调用两次。 第二次调用它会导致异常: 使用 SurfaceController 表单时,只能在 Http POST 的上下文中使用 UmbracoPageResult

我已经恢复了更改,所以我回到了在 PostReset 操作之后调用的 Reset ([HttpGet])。

所以问题仍然存在。我该如何解决这个问题? 我需要从 PostReset Action 返回结果。

【问题讨论】:

    标签: asp.net-mvc forms http-post umbraco


    【解决方案1】:

    答案给了我here

    所有功劳归功于香农·德米尼克

    post 操作不会为响应返回任何内容(这对我来说是新的)。 在第二次运行Reset action的post后,由于modelstate被维护,通过传递一个新实例化的model,这个model会继承POST action中处理的model的model state(PostReset)。

    在第二次调用重置操作期间,验证逻辑意味着它永远不会到达返回部分视图的地步。

    我暂时绕过了验证逻辑,果然显示了模型验证消息。

    【讨论】:

      【解决方案2】:

      这就是我解决这个问题的方法:

      1. 我为模型创建了扩展方法:

        public static class ExtensionMethods
        {
           public static void MapModel<T>(this WebViewPage<T> page) where T : class
           {
              var models = page.ViewContext.TempData.Where(item => item.Value is T);
        
              if (models.Any())
              {
                 page.ViewData.Model = (T)models.First().Value;
                 page.ViewContext.TempData.Remove(models.First().Key);
              }
           }
        }
        
      2. 控制器代码:

        [HttpPost]
        public ActionResult Index(MyModel model)
        {
            TempData.Add("MyModel", model);
            return RedirectToCurrentUmbracoPage();
        } 
        
      3. 部分查看代码:

         @using UmbracoTest.Extension
         @using UmbracoTest.Models
         @model MyModel
         @{
             this.MapModel<MyModel>();
          } 
        
         @using (Html.BeginUmbracoForm("Index", "Home", FormMethod.Post))
         { 
              <div>
                @Html.TextBox("Text", Model.Text )
              </div>
        
             <input type="submit" name="submit" value="Submit" />
         }
        

      【讨论】:

        【解决方案3】:

        我通过解决命名冲突修复了这个错误:

        • 确保 GET 和 POST 方法的名称不同
        • 确保控制器名称不与任何文档类型冲突

        【讨论】:

          猜你喜欢
          • 2019-09-26
          • 1970-01-01
          • 2016-02-22
          • 1970-01-01
          • 2018-10-09
          • 2016-06-07
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多