Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Graeme 19 posts 111 karma points
    Jul 08, 2014 @ 18:56
    Graeme
    0

    Surface controllers, actionlinks with a friendly URL

    Hi,

    Wondering if somebody could guide me in the right direction, I'm new to MVC and unsure on the umbraco surface controllers.

    From one of my views I have a Html.ActionLink("Link Name", "ActionName", "Controller", new {some params})

    In my surface controller

                   [HttpGet]
    public ActionResult ActionName(Params...)
    {
        var model = new MyModel();
    //   initiate model member
        return PartialView("PartialView", model);
    }
       [HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult ActionNamePost(MyModel model)
       {
           //model not valid, do not save, but return current umbraco page
           if (!ModelState.IsValid)
           {
               return CurrentUmbracoPage();
           }
           // model valid, save the properties
           SaveModel(model);
           var content = Services.ContentService.GetById(model.ContentPageId);
           TempData.Add("ThankYouMessage", content.GetValue("thankYouMessage").ToString());
           TempData.Add("Success""true");
    
           //redirect to current page to clear the form
           return RedirectToCurrentUmbracoUrl();
       }
    The partial view:
    
    @using MyProject.Controllers
    @model MyProject.Models.MyModel
    @{     Html.EnableClientValidation(true);     Html.EnableUnobtrusiveJavaScript(true); } @using (Html.BeginUmbracoForm<MyController>("MyAction")) {     @Html.AntiForgeryToken()         if (TempData["Success"] != "true")         {
            // out put form
            }
            else
            {
                <div class="wrapper">@Html.Raw(TempData["ThankYouMessage"])</div>
            }
    }
    That's the gist of it, may be some typo's don't worry about them. My question(s) are 
    1: How can I can my URL to be friendly so it doesn't show umbraco/Surface/MyController/MyAction?params
    2: When inspecting the html the action of the form is ActionName?params, I only really wanted this for the get to initialise my model I want the post to be ActionNamePost
    3: If I call my post the same as the get, it works but when failing on validation and trying to return CurrentUmbracoPage(); it fails with 

    Can only use UmbracoPageResult in the context of an Http POST when using a SurfaceController form

    any pointers or tips would be appreciated.
    Thanks

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies