Changing Culture on Drop Down Selection - asp.net-mvc

I have created an application where I require is to change the culture in the drop down selection.
This is my action method code.
public ActionResult SetCulture(string lang)
{
if (lang == "en")
return RedirectToAction("Index");
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang.Trim()); //.TextInfo.IsRightToLeft;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang.Trim());
List<Agent> lstMainAgent = new List<Agent>();
List<Agent> lstAgent = db.Agents.ToList();
for (int i = 0; i < lstAgent.Count(); i++)
{
lstAgent[i].AddressCity = Resources.Resource.AddressCity;
lstAgent[i].AddressCountry = Resources.Resource.AddressCountry;
lstAgent[i].AddressPostcode =Resources.Resource.AddressPostcode;
lstAgent[i].AddressStreet = Resources.Resource.AddressStreet;
lstAgent[i].Name = Resources.Resource.Name;
lstAgent[i].PhoneNumber = Resources.Resource.PhoneNumber;
lstMainAgent.Add(lstAgent[i]);
}
return View("Index", lstMainAgent);
}
This seems to be working but I have dynamic values list whose values are not added in the resource file and I am getting blank properties values in the view. I need to print all the values in the view. How can I achieve this?
Thanks in Advance

If it isnĀ“t in the resource file it will be blank. You could, however have a default resource file and specialized ones. If it has value you fill with the specialized if not the default.
public ActionResult SetCulture(string culture)
{
try
{
// set a default value
if (string.IsNullOrEmpty(culture))
{
culture = "en-US";
}
// set the culture with the chosen name
var cultureSet = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture =cultureSet;
Thread.CurrentThread.CurrentUICulture = cultureSet;
// set a cookie for future reference
HttpCookie cookie = new HttpCookie("culture")
{
Expires = DateTime.Now.AddMonths(3),
Value = culture
};
HttpContext.Response.Cookies.Add(cookie);
List<Agent> lstAgent = db.Agents.ToList();
foreach (Agent item in lstAgent)
{
item.AddressCity = Resources.Resource.AddressCity;
item.AddressCountry = Resources.Resource.AddressCountry;
item.AddressPostcode = Resources.Resource.AddressPostcode;
item.AddressStreet = Resources.Resource.AddressStreet;
item.Name = Resources.Resource.Name;
item.PhoneNumber = Resources.Resource.PhoneNumber;
}
return View("Index", lstAgent);
}
catch (Exception ex)
{
// if something happens set the culture automatically
Thread.CurrentThread.CurrentCulture = new CultureInfo("auto");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("auto");
}
return View("Index");
}

Related

Changing culture only changing the language of model items

I have implemented the culture in my Asp.net application. But what I want is to change the language of the entire page but it's changing only model items language.
CultureHelper.cs
public class CultureHelper
{
protected HttpSessionState session;
public CultureHelper(HttpSessionState httpSessionState)
{
session = httpSessionState;
}
public static int CurrentCulture
{
get
{
switch (Thread.CurrentThread.CurrentUICulture.Name)
{
case "en": return 0;
case "hi-IN": return 1;
default: return 0;
}
}
set
{
if (value == 0)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
}
else if(value==1)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi-IN");
}
else
{
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
}
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
}
}
}
BaseController.cs
protected override void ExecuteCore()
{
int culture = 0;
if (this.Session == null || this.Session["CurrentCulture"] == null)
{
int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Culture"], out culture);
this.Session["CurrentCulture"] = culture;
}
else
{
culture = (int)this.Session["CurrentCulture"];
}
// calling CultureHelper class properties for setting
CultureHelper.CurrentCulture = culture;
base.ExecuteCore();
}
protected override bool DisableAsyncSupport
{
get { return true; }
}
My model class
[Display(Name="Name",ResourceType =typeof(Resource))]
Language only changing for the model class properties. But I want to change the language for static/nonmodel properties too. Like I want to change button text too. I have added all contents in the resource file. How can I achieve this?
Add a resource file for every culture you want to support, e.g.
Resources.en.resx
Resources.hi-IN.resx
The framework will resolve which file to use based on the set CurrentCulture.
Resources.resx (without culture name) will be used as fallback if no culture specific file can be found.
Use the resource file to retrieve your translated strings, e.g. in your View.cshtml:
#using MyProject.Resources
<button type="submit">
#Resources.Submit #* this retrieves the translation for the key "Submit" *#
</button>

How to redirect view in separate target window

I am using visual studio 2012 and mvc4.
I want to open CertDet view in separate window
In index view I have used submit with post method.
My Controller -
[HttpPost]
[AllowAnonymous]
public ActionResult Index(ModelCertificate cert)
{
if (ModelState.IsValid)
{
dbRTCEntities objCon = new dbRTCEntities();
Mst_Catref data = objCon.Mst_Catref.Where(x => x.Catref == cert.Catref).FirstOrDefault();
if (data != null)
{
return RedirectToAction("CertDet", new { catref = data.Catref, Excise = cert.ExciseNumber, customerNm = cert.CustomerName });
}
else
ModelState.AddModelError("", "Catref not found");
}
return View();
}
public ActionResult CertDet(string catref, string Excise, string customerNm)
{
dbRTCEntities objCon = new dbRTCEntities();
Mst_Catref data = objCon.Mst_Catref.Where(x => x.Catref == catref).FirstOrDefault();
ModelCertificate cert = new ModelCertificate();
return View(cert);
}
You should need something like this on your View:
#Html.ActionLink("CertDet", "CertDet", new { catref = data.Catref, Excise = cert.ExciseNumber, customerNm = cert.CustomerName }, new {target = "_blank"})
This will render something like this in a new window;
CertDet

How to hide status query string from Url after updating from MVC Controller

I have a view which shows info according to a specific Id.
My Controller is:
public ActionResult ModifyWorkout(string WorkoutId, bool? Status)
{
Guid pkWorkoutId = new Guid(WorkoutId);
BLLWorkouts _objBLLWorkouts = new BLLWorkouts();
var WorkoutRow = _objBLLWorkouts.GetOneWorkout(pkWorkoutId);
WorkoutModel PageModel = new WorkoutModel
{
pkWorkoutId = WorkoutRow.pkWorkoutId,
WorkoutName = WorkoutRow.WorkoutName,
WorkoutNote = WorkoutRow.WorkoutNote,
Sessions = WorkoutRow.Sessions,
CmpFlag = WorkoutRow.CmpFlag,
tblWorkoutSessions = WorkoutRow.tblWorkoutSessions
};
ViewBag.UpdateStatus = Status;
return View(PageModel);
}
I always pass a Query String as Workout id for getting a single record. But when I update that records it is posting to another action:
public ActionResult UpdateWorkout(WorkoutModel model)
{
bool _status = false;
BLLWorkouts _objBLLWorkouts = new BLLWorkouts();
tblWorkout _row = new tblWorkout();
_row.WorkoutName = model.WorkoutName;
_row.WorkoutNote = model.WorkoutNote;
_row.Sessions = model.Sessions;
_row.pkWorkoutId = model.pkWorkoutId;
_row.CmpFlag = model.CmpFlag;
_status = _objBLLWorkouts.UpdateWorkout(_row);
return RedirectToAction("ModifyWorkout", new { WorkoutId = model.pkWorkoutId, Status = _status });
}
Now problem is that after updating my url becomes like this:
/ModifyWorkout?WorkoutId=438b6828-1a21-4ad0-9e40-485dda75b1f6&Status=true"
And Status message is shown even after refreshing the page due to &Status=true in Url.
<div >
#if (ViewBag.UpdateStatus != null)
{
if (ViewBag.UpdateStatus)
{
<span class="successmsgCommon">Workout updated successfully.</span>
}
else
{
<span class="errormsgCommon">Workout was not updated. Please try again.</span>
}
}
</div>
Is there any way to solve this?
Use TempData to send the value
TempData["Status"] = true;
Consume
if (TempData["Status"])
About TempData
TempData is used to pass data from current request to subsequent request means incase of redirection.
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session
You can use TempData for this:
TempData["Status"] = _status;
return RedirectToAction("ModifyWorkout", new { WorkoutId = model.pkWorkoutId });
ASP.NET MVC will store this value for the next request so you can read it in the redirected action.
if ((bool)TempData["Status"])
{
<span class="successmsgCommon">Workout updated successfully.</span>
}
else
{
<span class="errormsgCommon">Workout was not updated. Please try again.</span>
}

How to pass a parameter to your Controller, through a button click in MVC?

May someone assist me with how to tie up my parameters from a combobox to a parameter on one of my action's in my controller. I am using DevExpress control suite and this is my code:
[HttpPost]
public ActionResult Filter(string filterCriteria)
{
ViewData.Model = Repository.GetReports(filterCriteria);
return RedirectToAction("Index");
}
And this is my view :
<div style="float:right;padding-bottom:20px;" >
#using (Html.BeginForm())
{
<table><tr><td>
Filter by Keyword:</td>
<td>
#Html.DevExpress().DropDownEdit(
settings =>
{
settings.Name = "checkComboBox";
settings.Width = 210;
settings.Theme = Utils.CurrentTheme;
settings.Properties.DropDownWindowStyle.BackColor = System.Drawing.Color.FromArgb(0xEDEDED);
settings.Properties.ClientSideEvents.ValueChanged = "function(s, e) {e.customArgs[\"filterCriteria\"] = checkComboBox.Name.toString();}";
settings.SetDropDownWindowTemplateContent(c =>
{
#Html.DevExpress().ListBox(
listBoxSettings =>
{
listBoxSettings.Name = "checkListBox";
listBoxSettings.SkinID = "CheckComboBoxListBox";
listBoxSettings.CallbackRouteValues = new { Controller = "Report", Action = "Filter" };
listBoxSettings.Theme = Utils.CurrentTheme;
listBoxSettings.ControlStyle.Border.BorderWidth = 2;
listBoxSettings.ControlStyle.BorderBottom.BorderWidth = 1;
listBoxSettings.ControlStyle.BorderBottom.BorderColor = System.Drawing.Color.FromArgb(0xDCDCDC);
listBoxSettings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
listBoxSettings.Height = 100;
listBoxSettings.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
listBoxSettings.Properties.ClientSideEvents.SelectedIndexChanged = "OnListBoxSelectionChanged";
}
).BindList(Repository.GetKeywords())
.Render();
ViewContext.Writer.Write("<table style=\"width:100%\"><tr><td align=\"right\">");
#Html.DevExpress().Button(
buttonSettings =>
{
buttonSettings.Name = "buttonClose";
buttonSettings.Text = "Close";
buttonSettings.Style.Add("float", "right");
buttonSettings.Theme = Utils.CurrentTheme;
buttonSettings.ClientSideEvents.Click = "function(s, e){ checkComboBox.HideDropDown(); }";
}
)
.Render();
ViewContext.Writer.Write("</td></tr></table>");
});
settings.Properties.EnableAnimation = true;
settings.Properties.ClientSideEvents.TextChanged = "SynchronizeListBoxValues";
settings.Properties.ClientSideEvents.DropDown = "SynchronizeListBoxValues";
}
).GetHtml()
</td><td>
#Html.DevExpress().Button(
settings =>
{
settings.Name = "btnApply";
settings.ControlStyle.CssClass = "button";
settings.Text = "Apply";
settings.UseSubmitBehavior = true;
settings.Theme = Utils.CurrentTheme;
settings.Width = 80;
settings.RouteValues = new { Controller = "Report", Action = "Filter" };
}
).GetHtml()
</td><td>
#Html.DevExpress().Button(
settings =>
{
settings.Name = "btnClear";
settings.ControlStyle.CssClass = "button";
settings.Text = "Clear";
settings.ClientSideEvents.Click = "function(s, e){ ASPxClientEdit.ClearEditorsInContainer(); }";
settings.Theme = Utils.CurrentTheme;
settings.Width = 80;
}
).GetHtml()
</td></tr></table>
}
</div>
I have basically a gridview on the bottom i would like to filter with a drop down list above it. I invoke the action by using a button to go filter my collection. In this example, i was just trying to pass back the name of the control, i.e.
settings.Properties.ClientSideEvents.ValueChanged = "function(s, e) {e.customArgs[\"filterCriteria\"] = checkComboBox.Name.toString();}";
just to see if I am able to pass back any value but this failed. Can someone please elaborate on a way to do this?Thank you,
From what I gather, you want to retrieve the multiple items selected within the "checkListBox" ListBox on the Controller side.
If so, use the ListBoxExtension.GetSelectedValues method in the following manner:
[HttpPost]
public ActionResult Filter(string filterCriteria)
{
//listBoxSettings.Name = "checkListBox";
string[] values = ListBoxExtension.GetSelectedValues<string>("checkListBox");
filterCriteria = values...
ViewData.Model = Repository.GetReports(filterCriteria);
return RedirectToAction("Index");
}
I know this is an older question, but thought I might just add the use of viewbag which allows you to send data from controller to view, and vice versa. Here is an example from mdsn.
controller
public ActionResult Index()
{
ViewBag.MyMessageToUsers = "Hello from me.";
ViewBag.AnswerText = "Your answer goes here.";
return View();
}
view
#ViewBag.MyMessageToUsers
#Html.TextBox("AnswerText")

two models in a view - not working for me

I have created an entity data model from my database. however in certain areas of the application i need to pass two models. thus i create a third model which has as properties the objects of each required model.
In the scenario, i want to use one model just to show some data to the user and the other is to be populated by the user using form elements. therefore, i create a constructor in my custom model to populate it. here's the code:
THE CUSTOM MODEL
public class ordersModel
{
public ordersModel(order or)
{
this.prods = new order_products();
this.new_order = new order();
this.new_order.customer_id = or.customer_id;
this.new_order.my_id = or.my_id;
this.new_order.my_order_id = or.my_order_id;
this.new_order.order_date = or.order_date;
this.new_order.order_status_id = or.order_status_id;
}
public order new_order { get; set; }
public order_products prods { get; set; }
}
IT IS USED IN THE CONTROLLER AS FOLLOWS:
public ActionResult Create()
{
order or = new order();
// Store logged-in user's company id in Session
//or.my_id = Session["my_id"].ToString();
//do something to allow user to select customer, maybe use ajax
or.customer_id = "123";
or.order_amount = 0;
or.my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
// display date picker to select date
or.order_date = DateTime.Now.Date;
// fetch statuses from database and show in select list box
or.order_status_id = 1;
return View(or);
}
//
// POST: /Orders/Create
[HttpPost]
public ActionResult Create(order or)
{
using (invoicrEntities db = new invoicrEntities())
{
var temp = db.last_order_number.SingleOrDefault(p => p.my_id == or.my_id);
if (temp != null)
{
or.my_order_id = temp.my_order_id + 1;
if (ModelState.IsValid)
{
ordersModel ord = new ordersModel(or);
db.orders.AddObject(or);
temp.my_order_id = temp.my_order_id + 1;
//TempData["my_order_id"] = or.my_order_id;
db.SaveChanges();
return RedirectToAction("AddProducts", ord);
//return RedirectToAction("AddProducts", new { id = or.my_order_id });
}
return View(or);
}
return RedirectToAction("someErrorPageDueToCreateOrder");
}
}
public ActionResult AddProducts()
{
using (invoicrEntities db = new invoicrEntities())
{
//string my_id = TempData["my_id"].ToString();
//string my_order_id = TempData["my_order_id"].ToString();
string my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
int my_order_id = 1;
//Int64 my_order_id = Convert.ToInt64(RouteData.Values["order_id"]);
// Display this list in the view
var prods = db.order_products.Where(p => p.my_id == my_id).Where(p => p.my_order_id == my_order_id).ToList();
var or = db.orders.Where(p => p.my_id == my_id).Where(p => p.my_order_id == my_order_id).ToList();
if (or.Count == 1)
{
//ViewData["name"] = "sameer";
ViewData["products_in_list"] = prods;
ViewData["order"] = or[0];
return View();
}
return RedirectToAction("someErrorPageDueToAddProducts");
}
}
[HttpPost]
public ActionResult AddProducts(order_products prod)
{
prod.my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
// find a way to get the my_order_id
prod.my_order_id = 1;
return View();
}
THIS ALL WORKS OUT WELL, UNTIL IN THE "ADDPRODUCTS" VIEW:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<invoicr.Models.ordersModel>" %>
AddProducts
<h2>AddProducts</h2>
<%: Model.new_order.my_id %>
the above statement gives an error
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
what am i doing wrong here?
You seem to be passing the wrong model when redisplaying your Create view.
Try passing the ord instance which is of type ordersModel and which is what your view is strongly typed to:
public ActionResult Create(order or)
{
using (invoicrEntities db = new invoicrEntities())
{
var temp = db.last_order_number.SingleOrDefault(p => p.my_id == or.my_id);
if (temp != null)
{
or.my_order_id = temp.my_order_id + 1;
ordersModel ord = new ordersModel(or);
if (ModelState.IsValid)
{
db.orders.AddObject(or);
temp.my_order_id = temp.my_order_id + 1;
db.SaveChanges();
return RedirectToAction("AddProducts", ord);
}
return View(ord);
}
return RedirectToAction("someErrorPageDueToCreateOrder");
}
}
UPDATE:
Now that you have shown your AddProducts action you are not passing any model to the view although your view expects an ordersModel instance. So don't just return View();. You need to pass an instance of ordersModel:
if (or.Count == 1)
{
ViewData["products_in_list"] = prods;
ViewData["order"] = or[0];
ordersModel ord = new ordersModel(or[0]);
return View(ord);
}

Resources