I´m trying to render url action with javascript in an MVC project. I capture an event on my page which calls this function but I´m not sure how to call this certain URL.
Can anyone help me please? :)
function onDropDownChange(e) {
var url = '/Home/Index/' + e.value;
//What now...?
}
-----------Edited-----------------------
Here´s my controller action:
public ActionResult Index(int? id)
{
var tmpToday = DateTime.Now;
var today = new DateTime(tmpToday.Year, tmpToday.Month, tmpToday.Day, 0, 0, 0);
if (id != null)
{
var num = id.GetValueOrDefault();
var rentableUnits = new List<Unit>();
_unitLogic.GetAllRentableUnitsByArea(num, rentableUnits);
var allAvailabilities = new ShowAvailability[rentableUnits.Count];
for (int i = 0; i < rentableUnits.Count; i++)
{
var sTime = GetFirstDayOfMonth(today);
var eTime = GetLastDayOfMonth(today);
allAvailabilities[i] = new ShowAvailability(sTime, eTime.AddHours(23.0).AddMinutes(59.0), rentableUnits);
today = today.AddMonths(1);
}
var showAvailability = new List<ShowAvailability>(allAvailabilities);
return View(new HomeFormViewModel(showAvailability));
}
else
{
var allAvailabilities = new ShowAvailability[12];
for (int i = 0; i < 12; i++)
{
var sTime = GetFirstDayOfMonth(today);
var eTime = GetLastDayOfMonth(today);
allAvailabilities[i] = new ShowAvailability(sTime, eTime.AddHours(23.0).AddMinutes(59.0));
today = today.AddMonths(1);
}
var showAvailability = new List<ShowAvailability>(allAvailabilities);
return View(new HomeFormViewModel(showAvailability));
}
}
#
I´m using Telerik extension for my DropDownList which fires the javascript function, this is in fact a Razor View:
@(Html.Telerik().DropDownList()
.Name("DropDownList")
.Items(area =>
{
area.Add().Text("Öll svæði").Value("0").Selected(true);
foreach (Unit a in Model.Areas)
{
area.Add().Text(a.Name).Value(a.UnitID.ToString());
}
})
.HtmlAttributes(new { style = "width: 130px;" })
.ClientEvents(clev => clev.OnChange("onDropDownChange"))
)
Here´s the script:
function onDropDownChange(e) {
var url = '/Home/Index/' + e.value;
$.ajax({
type: "GET",
url: url,
data: {},
dataType: "html"
});
}
try:
That should get you where you want.
Another way to ensure you get the correct url regardless of server settings is to put the url into a hidden field on your page and reference it for the path:
Then you just get the value in your ajax call:
I have been using this for years to cope with server weirdness, as it always builds the correct url. It also makes keeping track of changing controller method calls a breeze if you put all the hidden fields together in one part of the html or make a separate razor partial to hold them.
I'm going to give you 2 way's to call an action from the client side
first
If you just want to navigate to an action you should call just use the follow
Notes: that you action need to handle a get type called
Second
If you need to render a View you could make the called by ajax
And the client called like this "Assuming that you're using jquery"
or
or
Update
In your ajax called make this change to pass the data to the action
UPDATE 2
You cannot do this in your callback 'windows.location ' if you want it's go render a view, you need to put a
div
in your view and do something like thisin the view where you are that have the combo in some place
in the javascript client
With this i think that you solve your problem
Within your
onDropDownChange
handler, just make a jQuery AJAX call, passing in any data you need to pass up to your URL. You can handle successful and failure calls with thesuccess
anderror
options. In thesuccess
option, use the data contained in thedata
argument to do whatever rendering you need to do. Remember these are asynchronous by default!jQuery's AJAX documentation is here.