In MVC Razor how to access selected drop-down value from layout in multiple controllers

Problem Statement:

How to access selected drop-down value from layout in multiple controllers while performing CRUD operations in views.

Dropdown List in Layout.cshtml:

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

Partial View for Dropdown:

@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);
    }

Now in one more controller for ex: Test.Controller i want to access this under Actionresult method.

Public ActionResult Create(LanguageDropdownModel objDropdown)
{
//I want to access the dropdown value languageId from the layout
} 

Suggest me some ways to do it???

Reference for this question(Continued from previous question): Click here to see previous question

Answers


You can use Session to store your dropdown value.

By looking at your previous question it seems that you are calling the below function at your controller side while the value of dropdown is being changed. So now in this function save your Language in session and than use that session value in each controller. I think this should work for you.

public JsonResult GetLanguage(int languageID)
{
    // Save LanguageId in Session here
    Sessions.LanugageID = 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;
}

you should store it in TempData, its once read only but there is option if you want it to be in TempData, you can keep it still, TempData internally uses Sessions but i prefer to use TempData on Session:

public JsonResult GetLanguage(int languageID)
{

    TempData["LanuguageID"] = languageID;

}

Now you can access it in some other controller action like this:

int languageId = (int)TempData["LanuguageID"];

and after reading it will be no more available in temp data, it will be destroyed, but you can keep it in temp data still if want by calling keep like this:

TempData.Keep("LanuguageID");

Need Your Help

Xcode codesigning (failed) warning : App Store rejected?

ios xcode certificate code-signing

I have an error that I can't solve... I correctly registered as an Apple Developer, been approved by Apple, obtained Development and Distribution certificates and WWDR intermediate certificate.

Remove "Object" key on a JSON object

php ajax json

I'm working on a suggest select (using selectize.js with the example customization.html without theme changer) and i have a problem trying to connect my database to this select