Copied to clipboard

Flag this post as spam?

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


  • user33 9 posts 56 karma points
    Aug 05, 2014 @ 15:50
    user33
    0

    How to Retrieve current pageID in Umbraco7 and mvc

    Website will have multiple pages and each page will have its own star rating and the corresponding score is saved in Umbraco Backoffice.

    I have a Javascript which will post the score(“onClick event” ) to a method in SurfaceController using URL generated by “Url.Action()”.

    In the controller post method ,  I am unable to get the current node using Umrbaco.getCurrent() method. Its returning nulls.

     However I am able to use Umrbaco.getCurrent() method successfully in another post method which is invoked “On form Submit” in the same surfacecontroller.

    Only difference I see between the 2 post methods is “Url.Action()” method vs  “On form Submit”.

    Since the star rating is on multiple pages on the website, I want to get the corresponding nodeid dynamically in the post method and apply the changes on the properties of that corresponding page.

    Suggest me a way to get Current node id , I do not want to use document name, because it can change.

     

     cshtml file

     

    ---------------------

     

               <div class="row" style="margin-bottom: 15px;">

     

                   <div class="col-sm-12">

     

                        <div id="star"> </div>

     

                       @Html.Hidden("MyURL", Url.Action("StarRate"))

     

                        <input id="value" type="text" />

     

    Surface Controller

     

    ------------------------

     

    public class StarController : SurfaceController

     

        {

     

            //

     

            // GET: /Star/

     

     

     

            public ActionResult Index()

     

            {

     

     

     

               // RegisterRenderModel model = new RegisterRenderModel(content);

     

               // RouteData.DataTokens["umbraco"] = model;

     

     

     

                return PartialView("Partial1", new StarModel());

     

            }

     

     

     

            [HttpPost]

     

            public ActionResult StarRate(StarModel model)

     

            {

     

                if (!ModelState.IsValid)

     

                    return CurrentUmbracoPage();

     

     

     

                       IPublishedContent content = Umbraco.TypedContentAtRoot().First();

     

               object value1 = content.GetProperty("totalScore");

     

                  object value2;

     

                  foreach (var child in content.Children)

     

                  {

     

                      var childNode = child;

     

                      value2 = child.GetProperty("totalScore");

     

     

     

                  }        

     

                     object v1 = content.GetProperty("totalScore");

     

     

     

                return PartialView("Partial1", new StarModel());

     

                       }

     

    .js file

     

    ----------------

     

             var myUrl = $("#MyURL").val();

     

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 05, 2014 @ 15:58
    Dan Lister
    0

    Hi,

    Are you sending the OnClick event via ajax at all? If so, your Surface Controller will have a null current page ID because the ajax request is not part of a normal page request. For example, it is requesting a method, not a page. You could always pass the current page ID to your Surface Controller via the ajax method if you have that available client side.

    Thanks, Dan.

  • user33 9 posts 56 karma points
    Aug 05, 2014 @ 22:25
    user33
    0

    Thanks Dan ,That would work.

Please Sign in or register to post replies

Write your reply to:

Draft