How to fill in an Editorfor as the login user name

Asked

Viewed 395 times

1

I have a View, where the user must fill in some fields, and has a last field that I left him disable, and would like that in this field appears the name of the user, but how do I fill?

Model:

public class CombustivelModels
{
    [Key]
    public int CombustivelId { get; set; }

    [Required]
    public decimal km_inicial { get; set; }
    [Required]
    public decimal km_final { get; set; }
    [Required]
    public decimal litros { get; set; }
    [Required]
    [DataType(DataType.Currency)]
    public decimal valor { get; set; }

    [Required]
    public string UserId { get; set; }
}

View:

        <div class="form-group">
             @Html.LabelFor(model => model.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
             <div class="col-md-10">
                @Html.EditorFor(model => User.Identity.Name, new { htmlAttributes = new { @class = "form-control", @disabled=""} })
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
             </div>
        </div>

Example:

inserir a descrição da imagem aqui

But when trying to save:

inserir a descrição da imagem aqui

1 answer

1


When you put a note of [Required] and uses the EditFor he will create the imput with data-val="true" that will already make this validation for you.

When you did the @Html.EditorFor(model => User.Identity.Name, he generated an Imput with no name other than UserId...

As there is no imput in the form with the name of UserId it will try to validate and will not be able to bind this property of your VM object and will put the message on the screen...

This message appears where you did @Html.ValidationMessageFor(model => model.UserId,

What I would do in this case is just display his name on an imput picking with User.Identity.Name and not having that property on the VM.

On the server side you take the logged in user and play in the database (I believe it is a CRUD that you are doing).

If I can’t clear the doubt, tell me I’ll get better.

UPDATING

In your Model you take the note as below:

Of:

    [Required]
    public string UserId { get; set; }

To:

    public string UserId { get; set; }

In the view you remove:

@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })

In the controller I believe you are getting one CombustivelModels and with the changes I suggested it will be filled, but with the property UserId null

In the controller code you take the user name and play in the property.

meuObjQueVeioDaView.UserId = User.Identity.Name
//....
//resto do código

It only confirms whether the User.Identity.Name it’ll work on the controller, I think it will, if you can’t tell me.

  • Man, I get it, but I don’t know how to fix it, you could point me out, or improve your answer?

  • @Thomaserichpimentel take a look and see if it got better.

  • Perfect. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.