locked
ASP Route Data attribute in HTML Actionlink RRS feed

  • Question

  • User-501297529 posted

    I have a href attribute using anchor tag helper asp-all-route-data. I want to change this to use HTML.ActionLink that has a onclick popup message but not sure how to do it.

    I have tried this so far:

     @Html.ActionLink("Set as Draft", "SetAsDraft", "Item", new { 
            FromRouteAttribute = "setAsDraftParams" },
                                             new { onclick = "return 
         confirm('Are sure you want to set this Item as a draft?');" }

    a href:

    <a asp-area="Admin" title="Set as Draft DOR" asp-controller="Dor" asp- 
       action="SetAsDraft" asp-all-route-data="setAsDraftParams">Set as Draft 
        DOR</a>

    Variable set in the View

      var setAsDraftParams = new Dictionary<string, string>
            {
                { "id", Model.DraftToView.ItemId.ToString() },
                { "returnViewName", 
            nameof(Web.Areas.Admin.Controllers.ItemController.Index) 
           }
            };

    I expect the HTML.ActionLink to return to the viewname set in the setASDraftParams variable

    Friday, July 26, 2019 12:52 PM

Answers

  • User475983607 posted

    I expect the HTML.ActionLink to return to the viewname set in the setASDraftParams variable

    setAsDraftParams is a string...

    @Html.ActionLink("Set as Draft", "SetAsDraft", "Item", new { FromRouteAttribute = "setAsDraftParams" }, new { onclick = "return confirm('Are sure you want to set this Item as a draft?');" }

    Start using the browser dev tools (F12) to review the HTML you are building.  

    If you are trying to add a querystring to the end of the URL then simply...

    @Html.ActionLink("Set as Draft", "SetAsDraft", "Item", new
    {
        id = Model.DraftToView.ItemId.ToString(),
        returnViewName = nameof(Web.Areas.Admin.Controllers.ItemController.Index)
    }, new
    {
        onclick = "return  confirm('Are sure you want to set this Item as a draft?');"
    }):

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 1:10 PM