6

I'd like to call the method "EditProject" of my controller "Project" with a parameter being the Id of the project.

I've seen many people using Ajax, but the problem is I'd like to call the controller which in turn will redirect the user to the View "EditProject", I don't want to stay on the same page

Here is the code I've tried before figuring out it doesn't work :

$('.edit-project').click(function (d) {
    var id = $(this).attr('data-projectId');

    $.ajax({
        url: '@Url.Action("EditProject", "Project")',
        data: { id: id },
        type: "GET"
    }).done(function() {
        console.log("Done");
    });
    return false;
});

I've also tried simply using

$.get("/Project/EditProject/" +id); 
window.location.href = '@Url.Action("EditProject", "Project")' + '/' + id;
window.location.href = ("/Project/EditProject/" +id);

but it returns a 404 error.

The method called is very simple :

[HttpGet]
[Authorize(Roles = "Professional")]
public async Task<IActionResult> EditProject(int id)
{
    Project p = await _unitOfWork.Projects.GetById(id);
     ViewData["Title"] = "Modification du challenge " + p.ProjectName;
     return View(p);
}

And as expected with Ajax, it does return the View, but it returns it as a response to the ajax query, and thus it doesn't redirect and show the View as I would like.

3
  • 2
    Is this a typo $.get("/Project/EditProject/ +id"); did you mean $.get("/Project/EditProject/" +id); Commented Mar 5, 2018 at 11:39
  • It looks to me like it just needs window.location.href = "/Project/EditProject/" + id; Commented Mar 5, 2018 at 11:42
  • Yes it was a typo, I've also tried the window.location.href, but it actually never calls the method from the controller. I've tried putting a breakpoint, and it never stops. Commented Mar 5, 2018 at 11:44

2 Answers 2

3

You simply want to redirect there instead.

window.location.href = '@Url.Action("EditProject", "Project")' + '/' + id

There may be an MVC way to pass your id to @Url.Action but I'm not too familiar with it - string concatenation should work.

The controller is possibly not understanding what /1 means. You may have to change it to the following:

[HttpGet("{id}", Name = "EditProject")]
[Authorize(Roles = "Professional")]
public async Task<IActionResult> EditProject([FromRoute] int id)
{
    Project p = await _unitOfWork.Projects.GetById(id);
     ViewData["Title"] = "Modification du challenge " + p.ProjectName;
     return View(p);
}

This tells MVC to expect your parameter to come from the route.

Sign up to request clarification or add additional context in comments.

3 Comments

It appears to not call the method from the Controller, my debug doesn't stop at the breakpoint inside, and I'm entirely sure there's no typo in the method name
Does the browser actually navigate somewhere when you trigger the click handler?
It does, it goes to localhost:44311/Project/EditProject/1 which is technically what I want, except it doesn't call the controller for some reason?
3

You can simply do this

window.location.href = '@Url.Action("EditProject", "Project",new { id = ID })';

you don't have to change your Action method routing if your default routing is alright.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.