Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  122] [ 5]  / answers: 1 / hits: 17201  / 11 Years ago, wed, december 4, 2013, 12:00:00

In my ASP.Net project I'm trying to redirect a page to other action after select a row table and click on a button. So I have this code:



JQuery:



function editItem(tableId, url) {
if ($(# + tableId + .selected).exists()) {

var thisRowId = $(# + tableId + .selected).attr(id);

window.location.replace(url, { operation: edit, carId: thisRowId });
}
};

//more code


View (ManagementCar):



@model myProject.Model.Car.Car[]

<table class=table id=tableCars>
<thead>
@*some code to header*@
</thead>
<tbody>
foreach (var item in Model)
{
<tr id=@(item.Id) onclick=selectRow('tableCars', '@(item.Id)')>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.OwnerName)
</td>
<td>
@(item.IsSold == true ? Yes : No)
</td>
</tr>
}
</tbody>
</table>
<br />
<button id=btEdit type=button class=disable onclick=editItem('tableCars','CarOperation')>Edit</button>


Controller:



[HttpGet]
public ActionResult CarOperation(string operation, int carId)
{
//some code...

return RedirectToAction(ManagementCar, Backoffice);
}


But I have a Server Error after redirect saying carId parameter is null. I debug my jquery and that parameter isn't null. I tried also doing



$.get(url, { operation: edit, carId: thisRowId });


instead



window.location.replace(url, { operation: edit, carId: thisRowId });


but it don't redirect.
How can I solve this?


More From » jquery

 Answers
11

Ok, I finally solved the problem with a new url routing config and the following code:



My RouteConfig:



routes.MapRoute(
name: ManipulatingCar,
url: {controller}/{action}/{operation}/{carId}
);


My JQuery:



editItem = function (tableId, url) {
if ($(# + tableId + .selected).exists()) {

var thisRowId = $(# + tableId + .selected).attr(id);

var fullUrl = url + /edit/ + thisRowId;

window.location = fullUrl;
}
};


Basically, controller action parameters must match with the configurations specified in RouteConfig.cs


[#73918] Monday, December 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trayvon

Total Points: 35
Total Questions: 117
Total Answers: 88

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;