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

匆匆过客 提交于 2019-12-25 03:06:24

问题


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


回答1:


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



回答2:


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");


来源:https://stackoverflow.com/questions/22354687/in-mvc-razor-how-to-access-selected-drop-down-value-from-layout-in-multiple-cont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!