0

I'm trying to generate a password input with Html.PasswordFor() but it seems to generate the wrong name.

This is my entire code:

@using System.Web.UI.WebControls
@using Duzan.Domain.Entities
@model IEnumerable<Korisnik>

@{
  ViewBag.Title = "Login";
}

@ViewBag.Nered



<div class="container">
    <div class="row">
        @foreach (var k in Model)
        {
            <div class="col-xs-6 col-sm-3" style="padding-bottom: 10px;">
                <div class="center-block" style="height: 150px; width: 150px; border: black solid 2px; text-align: center">
                    @k.Ime
                    @using (Html.BeginForm())
                    {
                        @Html.Hidden("Id", k.Id)
                        @Html.PasswordFor(m => k.Lozinka, new {name = "Lozinka", id = "Lozinka", placeholder = "lozinka", style = "width: 90%"})
                        <input type="submit" @*style="visibility: hidden"*@/>
                    }

                </div>
            </div>
        }
    </div>
</div>

This is the HTML generated:

<input id="Lozinka" name="k.Lozinka" placeholder="lozinka" style="width: 90%" type="password">
m_bale
  • 175
  • 1
  • 1
  • 15
  • PasswordFor() dont need you set the name or id as data attribute, it does it for you. Remove the name and id attribute. – aperezfals Apr 21 '17 at 19:17
  • @AlejandroPérezFals that is the problem, if I do that, it renders as , and obviously I want it to be id="Lozinka", not id="k_Lozinka" or "k.Lozinka" – m_bale Apr 21 '17 at 19:19
  • First refer [this answer](http://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) - you cannot use a `foreach` loop to generate form controls for a collection. But you view makes no sense - why do you have multiple forms (you can only submit one) that have a password input? What are you trying to do here? –  Apr 21 '17 at 21:25
  • @StephenMuecke I'm making a login page for a fixed amount of users, (similar to netflix who's watching), so you click on your account, enter password, and login – m_bale Apr 22 '17 at 11:44
  • Sorry, but its crazy to generate all that extra html, degrade performance, and in the process lose model binding, not be able to use client side validation and not be able to return the view if `ModelState` is invalid. Generate a link for each user, then on the click, either popup a single login form or redirect to a login page –  Apr 22 '17 at 11:46
  • @StephenMuecke Sorry I'm pretty new to this, can you try to explain how/why it's so bad to have a few more html inputs, and why i won't be able to use client side validation and the other things? – m_bale Apr 22 '17 at 15:27

1 Answers1

-1

The problem is that you are using a selector of your Model(m =>), including other object (k). Dont use the generic version of Password widget. Use

@Html.Password("lozinka", k.Lozinka, new { placeholder = "lozinka", style = "width: 90%"})
aperezfals
  • 1,341
  • 1
  • 10
  • 26
  • Thanks! It works now, but may I know why I shouldn't use the PasswordFor() in this case? – m_bale Apr 21 '17 at 19:29
  • PasswordFor uses a selector for your Model class. In this case, you Model is a IEnumerable. Then, you are trying to use PasswordFor with an object (Korisnik) that is not IEnumerable – aperezfals Apr 21 '17 at 19:32
  • That is just generating invalid html (duplicate `id` attributes) and creating multiple controls with the same `name` attribute which will never bind when the model is submitted. –  Apr 21 '17 at 21:19
  • @StephenMuecke I'm not sure if I understand you completely, but i did use 'null' instead of 'k.lozinka' so that it has no value, but the model binding works – m_bale Apr 22 '17 at 11:46
  • @m_bale, No, it is you who do not understand, and model binding does not work despite what you think (try and return the view based on `ModelState` errors for example) –  Apr 22 '17 at 11:49