简体   繁体   中英

How do I route a value of an IdentityUser (like Username) and not the entire object?

So my View looks like this:

@model List<ListUsers>



<div class="card-deck">
    @foreach (var user in Model)
    {
        <div class="card m-3" style="min-width: 18rem; max-width:30.5%">

            <div class="card-header">
                <h5>@user.FriendId</h5>
            </div>

            <div class="card-body">
                <h2>@user.FriendName</h2>
            </div>


            <div class="card-footer text-center">
                <a asp-controller="account" asp-action="showprofile" asp-route-id="@user"
                   class="btn btn-primary">ShowProfile</a>
            </div>
        </div>
    }
</div>

Even if I write

"asp-route-id="@user.FriendId"

(and I can see in the source code of the page in the browser that the ID is appended in the url) it hands the entire IdentityUser Object to the corresponding actionmethod. I just want the string.

This doesn't work either.

"asp-route-id="@user.FriendId.ToString()"

Thank you!

First confirm the field type of FriendId, receive the same type of parameter in the showprofile action, and receive the parameter with "id" variable name.

    public IActionResult showprofile(int id)//if it is string type,use 'string id' to receive
    {
       // do what you want 
        return View();
    }

You can also refer to this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM