Click here to Skip to main content
11,649,522 members (77,279 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
Dear Gents,

I have a dropdownlist which will pass the data to the query string and its working fine. I have another dropdownlist that i want to pass the value to the query string in url along with the previous dropdown, how can i do that in asp.net mvc

your help is much appreciated and save my hours.

Regards
Posted 19-Feb-13 10:03am
Comments
ryanb31 at 19-Feb-13 16:09pm
   
If you have code that does one dropdownlist into the query string what's the issue getting the 2nd one to be there as well?
Zoltán Zörgő at 19-Feb-13 16:12pm
   
It looks quite simple, so please paste here the relevant portion of the view.
Ahamed Azeem at 19-Feb-13 16:35pm
   
Dear Ryan,

That is the issue, when I select a dropdown, the url query string is replaced by the other value,

for ex
http://localhost:7919/Contract/Create?OccupationId=f73536b5-f18c-470c-b921-f542e5b39962

or

http://localhost:7919/Contract/Create?NationalityId=fb92cf56-716b-414a-99f6-c97ad440a8db

Either any one of the above value is coming not both. I want the url to keep the value and append the other one

regards
Ahamed Azeem at 19-Feb-13 16:15pm
   
Dear Ryan,

That is the issue, when I select a dropdown, the url query string is replaced by the other value,

for ex
http://localhost:7919/Contract/Create?OccupationId=f73536b5-f18c-470c-b921-f542e5b39962

or

http://localhost:7919/Contract/Create?NationalityId=fb92cf56-716b-414a-99f6-c97ad440a8db

Either any one of the above value is coming not both. I want the url to keep the value and append the other one

regards
ThePhantomUpvoter at 19-Feb-13 16:44pm
   
How can anyone tell what it is that you are doing wrong unless you post the code?
Ahamed Azeem at 19-Feb-13 16:47pm
   
@Html.DropDownList("NationalityId", ViewBag.NationalityId as SelectList, String.Empty, new { onchange = "location.href='/Contract/Create?NationalityId='+this.options[this.selectedIndex].value" })



@Html.DropDownList("OccupationId", ViewBag.OccupationId as SelectList, String.Empty, new { onchange = "location.href='/Contract/Create?OccupationId='+this.options[this.selectedIndex].value" })


This is the code for two dropdown values

regards
Zoltán Zörgő at 19-Feb-13 16:51pm
   
If you have onchange on both, you can not change both at once. Is the action you call different from the view this code is in?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
 $(function() {
     var collectParams = function(){
		var params = { OccupationId:$("#Occupation").val(), NationalityId:$("#Nationality").val() };
		if(params.OccupationId && params.NationalityId)
		{
			location.href=@Html.Action("Create","Contact") + "?" + jQuery.param(params)
		}
	 };
	 
	 $("#Nationality").change(collectParams);
	 $("#Occupation").change(collectParams);
    });
</script>
@Html.DropDownList("NationalityId", ViewBag.NationalityId as SelectList, String.Empty, new { id="Nationality" })
@Html.DropDownList("OccupationId", ViewBag.OccupationId as SelectList, String.Empty, new { id="Occupation" })
OnChange is called for both dropdowns, but it is triggering relocation only if both are specified.
  Permalink  
v3
Comments
Ahamed Azeem at 19-Feb-13 16:51pm
   
Dear Zoltan,

I have posted my view details in the previous comment, pls check and suggest
Zoltán Zörgő at 19-Feb-13 16:53pm
   
I would not use onchange, since than you can change only one of them. Put this logic on javascript side as suggested. Do you want to refresh the same view with different parameters or is the /Contract/Create an other one?
Ahamed Azeem at 19-Feb-13 17:01pm
   
actually i am brining another data from the database using nationality id and occupation id, that data will automatically get filled in the textbox once both of these values selected
Zoltán Zörgő at 19-Feb-13 17:25pm
   
See update
Ahamed Azeem at 24-Feb-13 1:11am
   
It shows below exception

"The controller for path '/Contract/Create' was not found or does not implement IController."}
Ahamed Azeem at 24-Feb-13 1:19am
   
Sorted out, but now it shows stack over flow exception in the script
Zoltán Zörgő at 24-Feb-13 1:30am
   
Should work. Paste following code in a stand-alone html file and open it in the browser:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(function() {
var collectParams = function(){
var params = { OccupationId:$("#Occupation").val(), NationalityId:$("#Nationality").val() };
if(params.OccupationId && params.NationalityId)
{
location.search=jQuery.param(params)
}
};

$("#Nationality").change(collectParams);
$("#Occupation").change(collectParams);
});
</script>
<select id="Nationality"><option value="">?</option><option value="1">1</option><option value="2">2</option></select>
<select id="Occupation"><option value="">?</option><option value="2">2</option></select>

You will see, that it is jumping only if both are specified. If you get such exceptions, you have some other bug, or you have used my code in the wrong way.
Ahamed Azeem at 24-Feb-13 1:44am
   
its working in a standalone but when i put it inside view its showing error and the page is not loaded
Zoltán Zörgő at 24-Feb-13 3:45am
   
What error? If your dropdown code snippets worked, mines should work also in that context. My dropdowns were only for demonstration. The javascript code is the interesting part.
Ahamed Azeem at 24-Feb-13 3:54am
   
Thanks a lot its worked, i removed action link in location and passed a constant string
Zoltán Zörgő at 24-Feb-13 5:58am
   
Good. But there is still a problem on your side. Html.Action is the way to create action links.
Ahamed Azeem at 24-Feb-13 6:05am
   
Yes you are right, but i am not creating the action link, am just append the query strings to the existing link

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

[removed, duplicated by the system, but I can't delete it]
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 354
1 F-ES Sitecore 344
2 Kornfeld Eliyahu Peter 319
3 CPallini 290
4 Maciej Los 269
0 OriginalGriff 1,577
1 Sergey Alexandrovich Kryukov 1,087
2 CPallini 1,015
3 DamithSL 996
4 jyo.net 994


Advertise | Privacy | Mobile
Web04 | 2.8.150804.4 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100