I can select a value from the drop down list and the js is triggered, I can see in firebug that it gets the selected value. However when I get to the mydll actionResult method I always end up with a null value inside the strSelected
I suspect the routing. I have tried making my own route DLLChangeCountry, but to no effect
An ajax post will not have the web page redirect to another page. What you need to do is in the dropdown's onchange event submitt a form so you can redirect to the other view
I can select a value from the drop down list and the js is triggered, I can see in firebug that it gets the selected value. However when I get to the mydll actionResult method I always end up with a null value inside the strSelected
samarmir
public ActionResult mydll(string strSelected)
{
return View("Index",someobject);
}
What;'s the meaning of that?
samarmir
$("#ddl").change(function() {
var strSelected = "";
$("#ddl option:selected").each(function() {
strSelected += $(this)[0].value;
});
var url = "/Dinners/mydll/" + strSelected;
More, in this javascript strSelected is defined inside the change function - it does not have value outside!
I have adjusted according to suggestion, however without difference. That is in firebug, I see that it has the changes put when coming into the actionresult, the result becomes null.
samarmir
Member
120 Points
336 Posts
MVC dropdownlist onchange
May 01, 2012 04:00 PM|LINK
Hi
I'm trying to submit a drop down list value to a action, but I keep losing the selected value.
This is my code.
The view, is called index.aspx
<select id="ddl">
<option value="united">united</option>
<option value="italy">italy</option>
<option value="chile">chile</option>
<option value="spain">spain</option>
</select>
the js.
<script type="text/javascript">
$(document).ready(function() {
$("#ddl").change(function() {
var strSelected = "";
$("#ddl option:selected").each(function() {
strSelected += $(this)[0].value;
});
var url = "/Dinners/mydll/" + strSelected;
$.post(url, function(data) {
// do something if necessary
});
});
});
</script>
the controller called dinners
public ActionResult mydll(string strSelected)
{
return View("Index",someobject);
}
And the global asax, which I think is the real problem.
//http://localhost:2535/135
routes.MapRoute(
"PrettyDetails",
"{Id}",
new { controller = "Dinners", action = "Details" },
new { Id = @"\d+" }
);
routes.MapRoute(
"UpcomingDinners",
"Dinners/Page/{page}",
new { controller = "Dinners", action = "Index" }
);
//------------------
////Dinners/mydll/italy
routes.MapRoute(
"DLLChangeCountry",
"Dinners/mydll/{country}",
new { controller = "Dinners", action = "mydll", country = "" } /*@"[a-z]+"*/
);
//------------------
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"OpenIdDiscover",
"Auth/Discover");
}
I can select a value from the drop down list and the js is triggered, I can see in firebug that it gets the selected value. However when I get to the mydll actionResult method I always end up with a null value inside the strSelected
I suspect the routing. I have tried making my own route DLLChangeCountry, but to no effect
Ken Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: MVC dropdownlist onchange
May 01, 2012 04:30 PM|LINK
An ajax post will not have the web page redirect to another page. What you need to do is in the dropdown's onchange event submitt a form so you can redirect to the other view
Space Coast .Net User Group
ignatandrei
All-Star
135142 Points
21676 Posts
Moderator
MVP
Re: MVC dropdownlist onchange
May 01, 2012 04:36 PM|LINK
What;'s the meaning of that?
More, in this javascript strSelected is defined inside the change function - it does not have value outside!
samarmir
Member
120 Points
336 Posts
Re: MVC dropdownlist onchange
May 01, 2012 04:49 PM|LINK
ok. so how do I change it in order to have the value carried all the way to the actionresult : mydll?
samarmir
Member
120 Points
336 Posts
Re: MVC dropdownlist onchange
May 01, 2012 05:16 PM|LINK
I have now rewritten the js code, but still have same problem. So its not the js.
var countryselected = "";
$(document).ready(function() {
$("#ddl").change(function()
{
var strSelected = "";
$("#ddl option:selected").each(function()
{
strSelected += $(this)[0].value;
});
countryselected = strSelected;
var url = "/Dinners/mydll/" + countryselected;
sendit(url);
});
function sendit(url)
{
$.post(url, function(data) {});
}
});
I still think its the routing thing
ignatandrei
All-Star
135142 Points
21676 Posts
Moderator
MVP
Re: MVC dropdownlist onchange
May 02, 2012 03:31 AM|LINK
It's the js.
var countryselected = "";
$(document).ready(function() {
$("#ddl").change(function()
{
var strSelected = "";
$("#ddl option:selected").each(function()
countryselected += $(this)[0].value;
});
//countryselected = strSelected;
samarmir
Member
120 Points
336 Posts
Re: MVC dropdownlist onchange
May 02, 2012 02:51 PM|LINK
Hi
I have adjusted according to suggestion, however without difference. That is in firebug, I see that it has the changes put when coming into the actionresult, the result becomes null.
var countryselected = "";
$(document).ready(function () {
$("#ddl").change(function () {
var strSelected = "";
$("#ddl option:selected").each(function () {
countryselected += $(this)[0].value;
});
var url = "/Dinners/mydll/" + countryselected;
$.post(url, function (data) {
// do something if necessary
});
});
});
samarmir
Member
120 Points
336 Posts
Re: MVC dropdownlist onchange
May 05, 2012 07:02 PM|LINK
@Ken Tucker
thanks for your answer,
can you kindly clarify. How exactly would I go on with this. If you can provide some sample code.