开发者

Getting an ID of a value from dropdown list

开发者 https://www.devze.com 2023-03-03 20:08 出处:网络
Hey, I Have three IQueryable lists which i concat together into one list to be displayed in the dropdown box. But now I want to get the id of what the user selected since there are 3 lists to choose

Hey, I Have three IQueryable lists which i concat together into one list to be displayed in the dropdown box. But now I want to get the id of what the user selected since there are 3 lists to choose from. Thanks

Example:

IQueryable<Store> stores= _storeRepository.FindAll().开发者_运维问答OrderBy(c => c.Description);
        var storeList = stores.ToSelectList("StoreId", "Description", viewModel.StoreId.ToString());

        IQueryable<Product> products = _productRepository.FindAll().OrderBy(j => j.Name);
        var productList = products.ToSelectList("ProductId", "Name", viewModel.ProductId.ToString());



var viewList = storeList.Concat(productList).ToList();
viewModel.Lookups = viewList; //display in dropdown


if you question was i have 3 lists and the user choose one how can i know which one he chosen

Answer : you give all three the same name and the value will change depend on which one the user have chosen


if your question was i want to send the id value with the selected list

Answer :

the server doesn't have any info about your html attributes

just the name attribute which maps to the action method parameter name and the value which maps to the parameter value , you can't know the id value and other attributes in the server

Example :

<input type="text" name="Title" id="SomeValue"/>

Will Map To :

public ActionResult Index(string Title)

the server will not recive id="SomeValue"

Solution :

What you can do is place a hidden field under every item with the value you want


instead of this way, after concatenating do this

  viewdata["viewList"]  = storeList.Concat(productList).ToList();

and view display items in dropdown like this

 <%= Html.DropDownList("viewlist_id", (List<SelectListItem>)ViewData["viewlist"])%>

now use a submit button (if want to post the data to same action else use actionlink with routvalues if redirecting to different action,I am using here submit button)

<input type="submit" name="submit" value="submit" /> 

and in your action you can retrieve the posted data

 var dropdown_id = Request.Form["viewlist_id"];

this way you will get the id of selected drop down. thanks


So i figured out how to have the values of different lists combined into one dropdown list will still being able to access its actual value or ID without concat or union- the code is a lot if anyone is interested i will go ahead and take the time to properly post it. Other than that, thank you everyone for offer your advise and help. Thanks


So this is how i went about my problem. In my Controller File - in my Get method after the button click this is what i did:

        resultSummaryViewModel.Value = value;
        resultSummaryViewModel.ReportFrame = new FramedViewModel();

        if(value !="")
        {
            string viewValue = value.Substring(0, value.IndexOf("|"));
            string viewType = value.Substring(value.IndexOf("|") + 1);

            resultSummaryViewModel.ReportFrame.SourceURL =
                WebPathHelper.MapUrlFromRoot(
                    string.Format(
                        "Reporting/ResultSummary.aspx?beginDate={0}&endDate={1}&Id={2}&viewType={3}",
                        resultSummaryViewModel.BeginDate, resultSummaryViewModel.EndDate, viewValue,
                        viewType));
        }
        var viewData = new Dictionary<string, string>();
        viewData.Add("Schools", "|allschools");
        viewData.Add("Classes", "|allclasses");

This is also connected to my display page aspx.cs which contains the actual lists i use to populate.

0

精彩评论

暂无评论...
验证码 换一张
取 消