-
-
Save rasmusfjord/070558245ae4ef91ec11 to your computer and use it in GitHub Desktop.
| @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Core.Models.IPublishedContent> | |
| <p>@Umbraco.GetDictionaryValue("Værelse")</p> | |
| public class BookingController : SurfaceController | |
| { | |
| public ActionResult GetRoomMarkup(int id) | |
| { | |
| return PartialView("booking-rooms", Umbraco.TypedContent(id)); | |
| } | |
| } |
The idea is i request the surface controller through ajax, and then it looses its culture/language for some reason.
Yup, if requested through ajax, culture info will be lost! I always pass language and set culture explicitely before calling partial view. Haven't found another way... must be there, jost don't know about it
If it works it works @netaddicts do you have a quick example of how you set it ? Just something like this ? System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
ajax call to surface controller
$.ajax({
url: "/umbraco/Surface/ProfessionalsSurface/DealerDetailSmall",
data: {
micrositeUrl: targetData.micrositeUrl,
id: dealerId,
language: lang,
nodeId: targetData.nodeId
}
});
action on the surface controller
[HttpGet]
public ActionResult DealerDetailSmall(string id, string micrositeUrl, string language, int nodeId)
{
var home = Umbraco.TypedContent(nodeId);
var languageShort = language.Substring(0, 2).ToUpper();
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
var model = new ProfessionalDetailSmallViewModel();
model.Dealer = detailResponse.Result;
model.MicrositeUrl = micrositeUrl;
model.ProfessionalsHome = Umbraco.TypedContent(home.GetPropertyValue<int>("ProfessionalsHomeNodeId"));
return PartialView("DetailDealerSmall", model);
}
+1 on Dirks solution i had same issue with ajax calls to controller I created a base surfacecontroller and inherited from that
public class AjaxSurfaceController : SurfaceController
{
protected readonly IUmbracoService _umbracoService = ServiceFactory.GetUmbracoService();
protected void SetCultureForAjax()
{
//we lose culture and dictionary wont get labels therefore set it on the thread
string code = _umbracoService.GetCultureCode();
//Get the culture info of the language code
CultureInfo culture = CultureInfo.CreateSpecificCulture(code);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
public class CommentSurfaceController : AjaxSurfaceController
{
if (ControllerContext.HttpContext.Request.IsAjaxRequest())
{
SetCultureForAjax();
}
//dostuff
}
I think you'll get the point...
Ignore the servicefactory and getculturecode bit you need to pass in the culture code you could get that from current node or its home page?
Arh roger got it ! :D
My solution ended up being like this :
View:
@Url.Action("GetRoomMarkup", "Booking", new { id = CurrentPage.Id, lang= Model.Content.GetCulture() })
Surfacecontroller Inheriter:
public class AjaxSurfaceController : SurfaceController
{
protected void SetCultureForAjax(string lang)
{
//Get the culture info of the language code
CultureInfo culture = CultureInfo.CreateSpecificCulture(lang);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
Surfacecontroller that inherites:
public class BookingController : AjaxSurfaceController
{
public ActionResult GetRoomMarkup(int id, string lang)
{
if (ControllerContext.HttpContext.Request.IsAjaxRequest())
{
SetCultureForAjax(lang);
}
return PartialView("booking-rooms", Umbraco.TypedContent(id));
}
}
From the log:
Thread 36] Error returning dictionary item 'Værelser'. Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Umbraco.Web.Dictionary.DefaultCultureDictionary.get_Item(String key)