0

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

Community
  • 1
  • 1
Vishal I P
  • 2,005
  • 4
  • 24
  • 41
  • 1
    What have you tried??You should be able to call by ID like it was there since the layout will render the HTML on the page. – Botonomous Mar 12 '14 at 15:11
  • Your question lacks context. Typically, one does not access values in a view, unless you're doing Ajax, and if you're using ajax, you simply access it like you would any other HTML control.. it has nothing to do with MVC, since JavaScript only works with the end generated HTML. – Erik Funkenbusch Mar 12 '14 at 16:07
  • @ErikFunkenbusch-Sorry my mistake.Instead of writing controllers i wrote views.I want to access the selected dropdown value defined in layout in one of the controller. – Vishal I P Mar 12 '14 at 17:03
  • @Anon-I have updated my question, suggest me some ways to do it. – Vishal I P Mar 12 '14 at 17:17
  • @ErikFunkenbusch-I have updated my question, suggest me some ways to do it. – Vishal I P Mar 12 '14 at 17:18

2 Answers2

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;
}
IT ppl
  • 2,626
  • 1
  • 39
  • 56
  • -Thanks i'm able to access the layout dropdown value through session in all controllers.One more doubt can i put the output(Resourcevalue and ResourceName) of GetLanguage() method in one session and access these values when i load some other views??? – Vishal I P Mar 13 '14 at 06:27
  • Yes. You can do it. You can store the whole object in Session. But I won't advise this as it would be very heavy for application. If this does solve your problem than don't forget to mark this as answer. So it would help other user in future. For more reading go through this http://stackoverflow.com/questions/769338/why-is-it-a-bad-idea-to-use-session-to-store-state-in-high-traffic-websites Hope this helps – IT ppl Mar 13 '14 at 07:01
  • Thanks for your advice.I will go through the link shared by you. – Vishal I P Mar 13 '14 at 07:20
1

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");
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160