How to refresh a page on dropdownlist value change

2019-07-19 09:10发布

问题:

I've got a common layout page for views. This layout page contains a dropdown list, which shows the list of available dates. What I want is to reload this page having changed the data param only. The controller, action and any other params should be somehow taken from the current request context.

If I wanted to do it manually on every view, I'd say it would be equal to

@Html.ActionLink(
     "",                                        
     "I need to have current action name here", 
     new { date = "The Value of the chosen SelectListItem" });

How can I achieve this? Or is there any different approach?

回答1:

Information about current action is available in RequestContext, you could access it directly:

@Url.Action(ViewContext.RequestContext.RouteData.Values["action"], 
         new { date = "The Value of the chosen SelectListItem" });

In your case I would add the url to option data attribute and then work with it with jQuery:

$('select#yourid').change(function() {
    document.location.href = $(this).find('option:selected').data('url'));
});​

How to fetch data attribute on dropdownlist change can be seen in this FIDDLE.



回答2:

Html:

<select id="mySelect" name="list" size="1"> 
   <option value="1">Option 1</option> 
   <option value="2">Option 2</option> 
</select> 
<span id="tag"></span> 

Javascript:

//cache the select and span elements  var mySelect =
 document.getElementById("mySelect"), 
 tag = document.getElementById("tag");    //when it changes  
 mySelect.onchange = function() { 
 //change the tag innerHTML checking the selected value of the select 
 tag.innerHTML = mySelect.value === "1" ? "some text" : "some other text";  }

That was an example. If you wan to load another page- call another JS function, or ajax control