Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a dropdown list with below values. when i click on url, i wanted to select the required value in the dropdown automatically.

select POR to fetch checklist
901:R Server Quarterly (Quarterly Update; Product/PFAM rename)
913:Visual Studio for MAC (media launch)



let's say if i want to select 901 value automatically, i wanted this value should be passed in the url and on clicking it should select the value.am using mvc.

my url is @urlaction("PORChecklist","Tables"). how do i select the value automatically from dropdown by using this url.

What I have tried:

I have used jquery in view for selecting the value automatically but i harcoded the value as of now.


function selectFromDropdown(selector, text) {
$(selector).find('option').each(function () {
if ($(this).text() == text) {
$(selector).val($(this).val());

return false;
}
})
}
setTimeout(function () {
selectFromDropdown('#PORList', '913:Visual Studio for MAC (media launch)')
},
it is working fine for me. but as requested i would need like wanted to pass the required value that need to be selected through url only.

here is the code snippet which holds the list of options in controller page.
public ActionResult PORChecklist()
{

List<string> dropdown_content = new List<string>();
dropdown_content = DOMEHelper.FufillmentChecklistPORs();
SelectList list = new SelectList(dropdown_content);

ViewBag.ListOfPORs = list;
return View();

}
Posted
Updated 18-May-17 2:52am
v2
Comments
F-ES Sitecore 16-May-17 6:49am    
Rather than using javascript specify what value you want selected when you create the SelectList object

http://stackoverflow.com/questions/1390830/mvc-set-selected-value-of-selectlist

I would say since you already have the item that needs to be selected in your URL you should just alter your action in your controller to accept it as the argument.

So if your URL is something like

/MyApp/PORChecklist/913:Visual Studio for MAC (media launch)

C#
public class MyAppController
{
	public ActionResult PORChecklist(string id)
	{
		List<string> dropdown_content = new List<string>();
		var dropdown_content = DOMEHelper.FufillmentChecklistPORs();
		SelectList list = new SelectList(dropdown_content);

		var selectedItem = list.FirstOrDefault(m=>m.Value == id);

		if(selectedItem != null)
		{
		   selectedItem.Selected = true;
		}    
		
		ViewBag.ListOfPORs = list;
		return View();
	}
}


If you name your parameter in your action something other than id you'll need to make sure you adjust your action or your routes to accommodate your different parameter name.

ex: /MyApp/PORChecklist/?selectedItem=913:Visual Studio for MAC (media launch)

Your action in your controller will need to look something like

C#
public ActionResult PORChecklist(string selectedItem)


This way you don't have to do your selection via javascript and it is handled by your controller keeping in line with the MVC conventions. I didn't run/test this code but the general idea is here to do what you need.
 
Share this answer
 
Comments
Member 13203555 17-May-17 3:57am    
am getting null value for var selected item when i execute. i have given id as 913:Visual Studio for MAC (media launch).is there any other to make the value selected when i pass the required id.
David_Wimbley 17-May-17 11:52am    
You might be having issues with the string 913:Visual Studio for MAC (media launch) in your URL. If possible, you could pass in an ID or might need to url encode/decode the value so it matches the actual drop down value.

Without seeing your code, that is the best i can tell you.
Member 13203555 18-May-17 5:47am    
it is working for me now. I able to select required value from dropdown automatically when i click on ulr /MyApp/PORChecklist/?selectedItem=913:Visual Studio for MAC (media launch)
var selectListItems = dropdown_content.Select(x => new SelectListItem() { Value = x, Text = x }).ToList();

foreach (var item in selectListItems)
{
if(item.Value == PORlist)
{
item.Selected = true;
break;
}
}

ViewBag.ListOfPORs = list;
return View();

I need one more help in the url, i dont want to hardcode selected item value like
PORChecklist/?selectedItem=913:Visual Studio for MAC (media launch). i just wanted to send the value like PORChecklist/?selecteditem = id
where id should contain the list of dropdown values when i mouse over on rows in a table. For example row 1 contains id 913 adn row 2 contains id 914.
when i click on id 913 it should redirect to PORChecklist/?selecteditem = 913:Visual Studio for MAC (media launch) and i when i click on row 2 id containing 914 it should click on PORChecklist/?selecteditem = 914:Visual Studio for SAP
Hi..thanks for your solution. Iam able to solve for the second question that i asked u. here is the below code.
in cs.html
<a class="visitedlink" target="_blank" href='@Url.Action("PORchecklist_list", "Tables" , new {PORID = @s.PorID })' >@s.OverAllTaskStatus%</a></div>

in controller
Quote:
public ActionResult PORChecklist_list(string PORID)
{

List<string> Desc = new List<string>();
Desc = WorkItemDescription.GetAllLaunchWorkItemDescription(PORID);

// Desc = WorkItemDescription.GetAllLaunchWorkItemDescription(913);
var list = "";

foreach (var a in Desc)
{
list = list + a;
}
return Json(list);
}


But am facing
Quote:
System.InvalidOperationException: This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
error.
I used
return Json(list, "text/plain");
and
return Json(list,JsonRequestBehavior.AllowGet);
as well ..am facing the issue while executing. Please help me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900