0

I need the value to change to a database field, it should show the name, but how do I do it?

<ul class="nav navbar-nav navbar-right">
    <li>
        @Html.ActionLink("Hello " + **User.Identity.GetUserId()** + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
    </li>
    <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>

I need to see another value, not the email field, like a "Nombres" using my IdentityModels:

public class ApplicationUser : IdentityUser
{

    public int Rut { get; set; }
    public string Nombres { get; set; }
    public string Apellidos { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaNacimiento { get; set; }
    public int Telefono { get; set; }
    public string Direccion { get; set; }
    public int Nacionalidad { get; set; }

User.Identity.GetUserId() shows the email, but I need it to show the "Nombres" data.

Diego Salazar
  • 43
  • 1
  • 10
  • Please do not post images of code. Please edit your question, and paste the relevant code in. Adding code and/or error messages as images creates more work for those that are trying to help you, and some cases may put off those that could help you. Please see this [answer](https://meta.stackoverflow.com/a/285557/5508175) as to why you shouldn't post images of code/errors. – Andrew Mar 20 '19 at 15:57
  • Oh, sorry. I'm a beginner on this platform. – Diego Salazar Mar 20 '19 at 16:06

2 Answers2

0

you can try this.

  1. create a model with the property you need. ex : UserDisplayName

    class NavbarModel {
        public string UserDisplayName { get;set; }
    }

  1. Bind your model in the HomeController Index action method.

    NavbarModel model = new NavbarModel();

    model.UserDisplayName = "some string";

  1. Pass the model to the partial view

  2. Inside the partial view you can access the property and display the value.


    @Html.ActionLink("Hello " + @Model.UserDisplayName  + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })

Please note this is just a workflow how you can implement the function.

  • I do not need to place a string, I need to place a field of my "identityModels", which is the user already logged into the system. – Diego Salazar Mar 20 '19 at 16:29
  • Can you please tell me what needs to show? If name of the user you can use User.Identity.Name – Sajitha Wattage Mar 20 '19 at 16:39
  • Edit the question again. Using IdentityModels, how do I show "Nombres" instead of email? thanks! – Diego Salazar Mar 20 '19 at 16:49
  • two ways. 1st one is @bhanu.cs 's answer. Or else you can put your properties as claims. Create an extension method to read the claims in views. call extension method in login partial. check this post https://stackoverflow.com/questions/35781423/how-should-i-access-my-applicationuser-properties-from-within-my-mvc-6-views – Sajitha Wattage Mar 21 '19 at 03:06
0

First you need to import Microsoft.AspNet.Identity.Owin namespace into your view.Attach the below statement on top of your view.

@using Microsoft.AspNet.Identity.Owin

Now you can get the value from OwinContext by invoking Usermanager context by firing FindMyName method on usermanager context which returns your ApplicationUser Object.

<ul class="nav navbar-nav navbar-right">
    <li>
        @Html.ActionLink("Hello " + HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindByName(User.Identity.Name).Nombres + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
    </li>
    <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
bhanu.cs
  • 1,345
  • 1
  • 11
  • 25
  • As per mvc pattern it is not a good idea to put application logic in view instead you can put application logic i.e getting the nombres field in controller action which returns a string and call Html.Action from your view – bhanu.cs Mar 21 '19 at 02:42