umbraco mvc surface controller, can't return view from HttpPost Action
This is how I solved this problem:
I created extension method for model:
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); } } }
Controller code:
[HttpPost] public ActionResult Index(MyModel model) { TempData.Add("MyModel", model); return RedirectToCurrentUmbracoPage(); }
Partial view code:
@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" /> }
The Answers were given to me here
All credit goes to Shannon Deminick
The post action does not return anything for the response (that bit was new to me). After the post when the Reset action is run the second time, since the modelstate is maintained, by passing a newly instantiated model, this model will inherit the model state of the model processed in the POST action (PostReset).
During the second time the Reset action was called, the validation logic meant it never gets to the point where it returns the partial view.
i temporarily bypassed the validation logic and sure enough the model validation messages were displayed.