How to pass Model property from ActionLink to Post method (with asp.net core)?

I’ve been recreating my project in asp.net core mvc from asp.net mvc.

When I used to delete a Tag object, I would send Tag.Name via the index page’s ActionLink to the Delete Post method.

@Html.ActionLink("Delete", "Delete", new { id = item.TagId, name = item.Name })

Then in the controller, on delete (Post), I would access Name like so:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, string name)
{
}

Though in asp.net core name is returned back as null in the Post method (though is correctly returned in the Get method).

I can retrieve name via <input asp-for="Name" type="hidden" /> within the Delete’s form Post, but I was wondering why I can’t retrieve it like I could with asp.net?

For reference here are the views

  • asp.net
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.Name)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Name)
    </dd>

</dl>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-actions no-color">
        <input type="submit" value="Delete" class="btn btn-default" /> |
        @Html.ActionLink("Back to List", "Index")
    </div>
}
  • asp.net core
<dl class="row">
    <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.Name)
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.Name)
    </dd>
</dl>

<form asp-action="Delete">
    <input type="submit" value="Delete" class="btn btn-danger" /> |
    <a asp-action="Index">Back to List</a>
</form>

Thank you

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As Jeremy Lakeman says, we could pass the parameter as query string to post method, but you should set it in the form tag’s action method.

I suggest you could try to modify the asp.net core codes to set the action and method attribute value according to the model name and id.

More details, you could refer to below codes:

<dl class="row">
    <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.Name)
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.Name)
    </dd>
</dl>
@*Modify the action url to your controller/action*@
<form action="home/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3652535a5342530958575b530b767b5952535a1878575b53">[email protected]</a>&<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="650c015825280a0100094b2c01">[email protected]</a>" method="post">
    <input type="submit" value="Delete" class="btn btn-danger" /> |
    <a asp-action="Index">Back to List</a>
</form>

Result:

How to pass Model property from ActionLink to Post method (with asp.net core)?

Method 2

From my understanding, you can’t send a Model property from an ActionLink to a POST method.

e.g ActionLink on the Index page that directs to the Delete page.

@Html.ActionLink("Delete", "Delete", new { id = item.TagId, name = item.Name })

You can only pass to a GET method.

With asp.net, apparently the Model property (sent through the ActionLink to the Delete page) was automatically bound as a query parameter, meaning it could be accessible in the POST method.

To do the same with asp.net core, I believe I can just add the Model property as a query parameter in the Form when Posted, like so:

<form asp-action="Delete" asp-route-name="@Model.Name"> </form>

This produces the html:

<form action="/Tag/Delete/9?name=Orange" method="post"> </form>

Which is then accessible in the POST method

public ActionResult Delete(int id, string name) { }

@Brando Zhang I was having trouble using your method, but I think the result is the same.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x