Change label display name labels, based on the val

2019-09-07 08:42发布

问题:

Problem Statement: I want to change the display name of labels(@Html.LabelFor) in Razor view of MVC based on the display names which i get from db.

I have added the dropdown list of languages in the _Layout.cshtml

 <li>@Html.Action("Index", "LanguageDropdown", new { languageid = Request["languageId"] })</li>

I have created one partial view for drop down:

@model ALCMS.Web.Models.Master_or_Configuration.LanguageDropdownModel
    <script type="text/javascript">
        function GetLanguage() {
            var languageId = $('#LanguageId').val();
            var Url = "@Url.Content("~/MasterConfigGeneral/GetLanguage")";
            $.ajax({
                url: Url,
                dataType: 'json',
                data: { LanguageId: languageId },
                success: function (data) {
                }
            });
        }
        </script>
    <div style="display:inline-block">

   @Html.DropDownListFor(l => l.LanguageID, new SelectList(Model.Languages, "Value", "Text"), "Select Language", new { id = "LanguageId" ,onchange="GetLanguage()" })
        </div>

Partial View Controller:

public ActionResult Index(string languageId)
    {
        //return View();

        var languages = dbEntity.LookupLanguages;
        var model = new LanguageDropdownModel
        {
            LanguageID = languageId,
            Languages = languages.ToList().Select(l => new SelectListItem
            {
                Value = Convert.ToString(l.LanguageID),
                Text = l.Name
            })
        };
        return PartialView(model);
    }

In Controller Json Result method:

   public JsonResult GetLanguage(int languageID)
        {
            JsonResult jsResult = new JsonResult();
            objdbGlobalTenant.ddlLanguage = (from lsr in dbEntity.LocaleStringResources
                                             where lsr.LanguageID == languageID

                                             select new SelectListItem()
                                             {
                                                 Text = lsr.ResourceValue,
                                                 Value = lsr.ResourceName

                                             }).Distinct().ToList<SelectListItem>();

            //ViewBag.Language = objdbGlobalTenant.ddlLanguage;
            jsResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            return jsResult;
        }

Now everything is working fine.I'm able to get the selected langaugeID in Json Result method in Controller based on the change event of Language dropdown. Based on this Language ID i'm getting display names(ResourceValue) which i need to apply for the particular view.

Problems:

  • 1>After getting the display names from db how to change display names of particular view when language change event triggers.?? For ex:Currently i'm seeing the Create.CSHTML. Now if i change the language dropdown it should trigger Json Event in controller and after getting values it should apply the values on the view which it got from db.

Note: Dropdown is in Layout.cshtml(like master in .aspx)

  • 2>Drop-down which i placed in Layout.cshtml is getting refreshed every time new view is loaded which inherits(layout.cshtml).How to make the controller to retain it's state during postback??

  • 3>How to get the selected drop-down item from the layout in multiple
    Controllers,to change the display name in each view based on the langaugeid of dropdown in layout

How to do this??If i'm doing wrong suggest me some other ways...

回答1:

Below are the suggestions :

Issue 1 : You may keep one attribute in each label which identifies them uniquely.

Your HTML should render like following

<!-- For English -->
<label label-unique-name="Name">Name</label>
<label label-unique-name="Surname">Surname</label>

<!-- For French -->
<label label-unique-name="Name">nom</label>
<label label-unique-name="Surname">nom de famille</label>

<!-- For Spanish -->
<label label-unique-name="Name">nombre</label>  
<label label-unique-name="Surname">apellido</label>

Here label-unique-name is your attribute, which will remain fixed for each language. Now when you change the language from dropdown you will bring the values like below.

<!-- For English -->
<label-unique-name:"Name",label-value:"Name">;<label-unique-name:"Surname",label-value:"Surname">

<!-- For French -->
<label-unique-name:"Name",label-value:"nom">;<label-unique-name:"Surname",label-value:"nom de famille">

<!-- For English -->
<label-unique-name:"Name",label-value:"nombre">;<label-unique-name:"Surname",label-value:"apellido">

Please note : this is for understanding only, it's not a JSON.

Now using jQuery go through each label and replace the label's value. Hope it'll help you.

Issue 2 :

You can save the selected language's value in session, and generate your dropdown accordingly.

@Html.DropDownListFor(l => l.LanguageID, new SelectList(Model.Languages, "Value", "Text"), !string.isNullorEmpty(HttpContext.Current.Sessions["Language"]) ? HttpContext.Current.Sessions["Language"] : "Select Language", new { id = "LanguageId" ,onchange="GetLanguage()" })