Search code examples
c#ajaxmodel-view-controllersyntax-errorquote

Url redirect ajax call quotes within quotes


I'm populating a list from an ajax call including a button that will have a unique id assigned to it to redirect to a page.

The problem I'm having is that there is so many quotations in it that it's not reading properly

  row.append("<td><button onclick="location.href='@Url.Action("UserForm\\{" + user[i].id +"", "Request")'" >Edit</button</td>);

I've tried a few different variations of the quotation marks but having no luck.

Please help.

UPDATE:

I've got it to work for the general redirect but without the unique id by changing quotes to back ticks:

row.append(`<input type="button" value="Edit" onclick="location.href='@Url.Action("UserForm",  "Request")'" />`);

I'm still however needing to add on /{user[i].id} after UserForm. Having no luck adding a variable into it without causing syntax issues.

userform source code:

[RoutePrefix("Request")]
public class NewUserController : Controller
{
        [Route("UserForm")]
        [CustomAuthorize(Roles = UserRole.Any)]
        public ActionResult ManagerNewUserForm()
        {
            return View("~/Views/Request/UserForm.cshtml");
        }
}


Solution

  • Fixed it.

    So I was mixing html and cshtml up.

    the solution that worked:

     row.append("<a class='btn btn-default' href='" + '@Url.Action("UserForm", "Request")' + user[i].Id + "'>Edit</a>");