X-D*_*Dev 9 forms asp.net-mvc umbraco http-post
我创建了一个Surface控制器,其动作使用@ Html.Action(...)调用.
@ Html.Action调用在宏部分视图中完成,宏使用富文本编辑器包含在页面内容中.
(我是新手,所以如果我以错误的方式处理事情,请告诉我.)
Surface控制器有一个GET和一个POST动作,但它是在宏部分中调用的get动作.
获取操作呈现正常,在表单中输入任何数据都将使模型状态无效(这正是我目前正在测试的).
提交表单(没有输入数据)意味着我可以进入我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
@model UmbracoExt.Models.ResetPasswordSurfaceModel
@using (Html.BeginUmbracoForm("PostReset", "ResetPasswordSurface")) {
@Html.EditorForModel()
<input type="submit" value="Submit" />
}
Run Code Online (Sandbox Code Playgroud)
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@Html.Action("Reset", "ResetPasswordSurface")
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
从"重置操作"中删除[HttpGet]属性表明,在调用PostReset操作后,还会调用"重置"操作.
将PostReset重命名为Reset并将httpget属性重新添加到原始Reset Action会导致调用post动作两次.第二次调用它会导致异常:在使用SurfaceController表单时,只能在Http POST的上下文中使用UmbracoPageResult
我已经恢复了更改,所以我回到了在PostReset操作后调用Reset([HttpGet]).
所以问题仍然存在.我该如何解决这个问题?我需要从PostReset Action返回结果.
mil*_*los 10
这就是我解决这个问题的方法:
我为模型创建了扩展方法:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)控制器代码:
[HttpPost]
public ActionResult Index(MyModel model)
{
TempData.Add("MyModel", model);
return RedirectToCurrentUmbracoPage();
}
Run Code Online (Sandbox Code Playgroud)部分视图代码:
@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" />
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
15542 次 |
| 最近记录: |