简体   繁体   English

如何路由 IdentityUser 的值(如用户名)而不是整个对象?

[英]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. (我可以在浏览器页面的源代码中看到,该 ID 附加在 url 中)它将整个 IdentityUser 对象交给相应的操作方法。 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.首先确认FriendId的字段类型,在showprofile action中接收same type参数,接收带有"id"变量名的参数。

    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 .你也可以参考这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过用户名获取 IdentityUser - How to get IdentityUser by Username 如何省略IdentityUser对象的某些字段 - How can I omit certain fields of IdentityUser object 如果用户已登录,我如何访问当前的 IdentityUser 属性? - How do I access current IdentityUser properties if a User is logged in? 如何在实体框架应用程序中播种 IdentityUser 引用? - How do I seed IdentityUser references in an Entity Framework application? 如何通过 ASP.NET 核心中的 api controller 更新 IdentityUser 的自定义属性? - How do I update a custom property of an IdentityUser through api controller in ASP.NET Core? IIdentity,IPrincipal,OWIN,IdentityUser和IUser如何 <string> 适合在一起? - How do IIdentity, IPrincipal, OWIN, IdentityUser and IUser<string> fit together? 如何使用用户管理器<identityuser>与用户存储<identityuser> ?</identityuser></identityuser> - How to use UserManager<IdentityUser> with UserStore<IdentityUser>? 如果我从 IdentityUser 继承,如何获取当前用户 ID? - How to get current user id, if i inherits from IdentityUser? 如何在ASP.NET Web API路由装饰中指定对象? - How do I specify an object in ASP.NET Web API route decorations? 如何添加其他路线? - How do I add an additional route?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM