Tuesday, November 29, 2016

Async calls (JSON) with sitecore



Routes.MapRoute(“AccountUpdateEmail”, “account/updateemail”, new {controller = “Account”, action = “UpdateEmail”});

that account/updateemail thing is an async call, not something called on page load. It appears to be working for me with a minor price to pay: registering each custom route as shown above.

In the view I have the following:

$("#emailSave").click(function () {
      //Make an async call to an action method
      var model = @Html.Raw(Json.Encode(Model));
      model.Email = $("#Email").val();

      var urlString = "/account/updateemail/?email=" + model.Email;
      //alert(urlString);

      $.getJSON(urlString, function (data) {
        if (data.Message === "success") {
          $("#showEmail").css('display', 'none');
          $("#currentEmail").text(data.Email);
          //display success message
          $("#yei").css('display', 'block');
        } else {
          //show error message
          $(".good success-bgd error").css('display', 'block');
        }
      });

    });



REPORT #2. Components added to a page. Success. I made two partial views, both use the default sitecore MVC rendering model which implies that you can either read sitecore data from current item or datasource on your rendering. I added them to presentation details of my navigable item. Left first one without datasource and added a datasource for the other one.

@model Sitecore.Mvc.Presentation.RenderingModel
<div>
  Data comes from current item: @Model.Item.Name
</div>

@using Sitecore.Mvc
@model Sitecore.Mvc.Presentation.RenderingModel
<div>
  Data comes from a datasource: @Html.Sitecore().Field("Title")
</div>


No comments:

Post a Comment