开发者

How to pass CultureInfo to partialView for ajax call?

开发者 https://www.devze.com 2023-04-03 05:14 出处:网络
I have a viewpage and a partialview on that. For default request-when page is called- CultureInfo is set in base class of viewPage. I have created this custom base class which initialize the culture d

I have a viewpage and a partialview on that. For default request-when page is called- CultureInfo is set in base class of viewPage. I have created this custom base class which initialize the culture depending on a cookie and does some other stuff as well. There is a search button on the view page which updates a partial view on the page with Ajax call (jquery Ajax call). There is separate action for this partial view update call which returns partial view. The problem is partial view do not have any base class and when resources are displayed on partial view they are displayed for default culture and not the current culture. What would be best approach to set cultureInfo when partial view is called from jQuery Ajax call?

I have this mvc application as part (area) of bigger existing applicati开发者_运维技巧on so I can not touch session_start.


I'm not sure why you need to pass the culture around, can't you get it directly from your PartialView using a helper method?

@YourApp.CultureHelper.GetCurrentCulture()

which is just calling

public static string GetCurrentCulture()
{
   return Thread.CurrentThread.CurrentCulture.Name;
}

If you are doing any work with internationalization and ASP.NET MVC I highly recommend reading through these two excellent posts by Nadeem Afana

  • ASP.NET MVC 3 Internationalization
  • ASP.NET MVC 3 Internationalization - Part 2 (NerdDinner)

Very well written and explains everything you need to know about internationalization.


You can send the culture info to your partial view as model with string array.

I guess you have handled the ajax call so I am just gonna show you the way of passing the value. The below one is your controller :

public ActionResult Index() { 

    return PartialViewResult("samplePartial", new string[] { "en" });

}

And this is your partial view :

@model string[]

@if(Model[0] == "en") { 

    @:Culture is "en"

} else { 

    @Culture is different than "en"

}

Also this can be also rendered inside your view as indicated below :

@Html.Partial("samplePartial", new string[] { "en" })
0

精彩评论

暂无评论...
验证码 换一张
取 消