简体   繁体   中英

MVC 3 Page navigation and passing parameters with javascript

i have my main page and also two partial views. One partial view is a menu and the other is a telerik grid. What i want to achieve is selecting a row in the grid and when i click a button in the menu i want the page to navigate to that action passing the selected row (id). i want to refresh the entire page and not only the div with the grid.

I tried using document.location = "/Pedido/DetalhePedido/" + id; but i don't receive the id n the controller. I also tried using $.get('@Url.Action("detalhePedido", "Pedido")', data, function (result) { }); usually i use this to refresh a div and i can't seem to make this work with the entire page (and it probably shouldn't ).

Wich methods do you usually use in your web apps to reproduce this sort of behaviour?

Because clicking on the row happens on the browser, you can't depend on anything from the controller or model unless it's already somewhere in your original model. I implement this by adding a hidden Id column to the grid and model it uses for rendering, then add client events and handlers to the grid and view. Check out the samples provided for the Grid under Client-Side Events for some of the different client events you can take advantage of.

On your grid, first add the Id column (hidden if you like):

.Columns(columns =>
{
   columns.Bound(o => o.Id).Hidden(true);
}

Then add ClientEvents and wire up onRowSelect like so:

.ClientEvents(events =>
{
    events.OnRowSelect("onRowSelected");
}

Then add a function to handle this event like so:

function onRowSelected(e) {
    var id = e.row.cells[0].innerHTML;
    window.location = "Something/Details/" + id;
}

UPDATE

Sounds like you are doing everything right on the client. The problem is likely elsewhere, in your Action or Bindings. But to be certain, take a look at what /Pedido/DetalhePedido/" + id actually is with an alert before venturing down that path. You should be able to take that and enter it directly into the url of your browser and hit your action as long as your action is correctly defined, accepting an int called id.

If its still a problem, you need to look at you action. Is it marked for Post? If it is Window.Location wont work because it's not a post. Is the argument it accepts named Id and of type int? If not, should it be? Have you changed your Routes, and if so does your Url match any routes defined?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM