this is my markup: the ajax is supposed to get called when there is a change to my dropdown selection:
this is my ajax jquery:
<script>
$("#AffiliateList").change(function (e) {
var textval = $("AffiliateList :selected").text();
var selected = $("#AffiliateList :selected").val();
$.ajax({
type: "GET",
url: "Website/Controllers/FindAnAffiliateController/getAffiliate",
data: { id : selected },
content: "html",
success: function (response) {
$("#affiliateDetail").empty();
$("#affiliateDetail").append(response);
}
})
})
</script>
This is my controller:
namespace Website.Controllers
{
public class FindAnAffiliateController : Controller
{
// GET: findAnAffiliate
public ActionResult Index()
{
return View();
}
public ActionResult FindAnAffiliate()
{
var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
var dataSourceItem = Sitecore.Context.Database.GetItem(dataSourceId);
var model = new AffiliateViewModel();
model.title = dataSourceItem.Fields["Title"].Value;
model.description = dataSourceItem.Fields["Description"].Value;
model.affiliateList = model.affiliates();
var selectedItem = model.affiliateList.FirstOrDefault();
var affiliateDetail = new Affiliate();
var affiliateItem = Sitecore.Context.Database.GetItem(selectedItem.Value);
affiliateDetail.title = affiliateItem.Fields["Affiliate Name"].Value;
var linkUrl =(Sitecore.Data.Fields.LinkField) affiliateItem.Fields["Affiliate Link"];
affiliateDetail.linkText = linkUrl.Text != "" ? linkUrl.Text : "No Link Available";
affiliateDetail.linkUrl = linkUrl.Url != "" ? linkUrl.Url : "#";
affiliateDetail.description = affiliateItem.Fields["Description"].Value;
model.affiliate = affiliateDetail;
return View(model);
}
public ActionResult getAffiliate(string id)
{
return PartialView();
}
}
}
and finally my RouteConfig
namespace Website
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
and here is my PartialView that should be returned:
<h4>Testing this stuff</h4>
<p class="media-link">www.alabamawildlife.org</p>
<p>The Alabama Wildlife Federation (AWF) is the oldest and largest non-profit conservation organization in Alabama. The AWF was established by sportsmen in 1935 to promote the conservation and wise use of our wildlife and natural resources and to ensure a high quality of life for future generations of Alabamians.</p>
<div class="col-xs-6">
<p class="uppercase-headline text-center">Co-certification of Certified Wildlife Habitat</p>
<img class="center-block" src="images/cwh-icon.png">
</div>
<div class="col-xs-6">
<p class="uppercase-headline text-center">Ranger Rick Subscription</p>
<img class="center-block" src="images/ranger-rick-logo-regional.png">
</div>
I am running on my localhost, I don't know if that is part of the problem, but outside of Sitecore, I have build ajax commands off of regular MVC applications with no issues.
Website/Controllers/FindAnAffiliateController/getAffiliate
in your js to/FindAnAffiliateController/getAffiliate
Sitecore.Mvc.Controllers.SitecoreController
, and then try to hit it at/api/sitecore/FindAnAffiliateController/getAffiliate