0

I have been searching for this but I cannot seem to find an answer on why its happening. I think that if I can understand why this is happening I can prevent it in the future.

So I have a basic Edit View that looks something like:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>EditUserViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Username,  new { @class = "form-control",disabled="disabled"} )
                @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.Role, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Role, new SelectList(@Model.Roles, Model.Role),  new { @class = "form-control" } )
                @Html.ValidationMessageFor(model => model.Role, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Partial("_SaveButtons", new SaveButtonsViewModel("Admin","Index", ""))
        </div>
    </div>
}

Please note that I added the Item in question(Username) after I created the View.

so when I go to click "Save" and it does a Post, the username is always null. I Started looking into this and I noticed that it isn't in the form data:

enter image description here

So now my question, why would this not be added to the form when it is inside the form, and I am using @Html helper methods?

4
  • 1
    1. Because it's disabled, it isn't part of the form data. 2. Do you really want your client to determine the Role of the user? Commented Aug 22, 2018 at 14:04
  • 1, so because its disabled it will not be part of the form? 2: I do want the admin to be able to edit the role of the user, why wouldn't I want that? Commented Aug 22, 2018 at 14:06
  • 1. Yes. 2. In this case I assume it's fine. Commented Aug 22, 2018 at 14:07
  • Input element with disabled attribute will not send as form data, hence you should use readonly instead: @Html.TextBoxFor(model => model.Username, new { @class = "form-control", @readonly = "readonly" }). Commented Aug 23, 2018 at 2:32

1 Answer 1

1

Any input element which has disabled attribute enabled will not be submitted as part of form data, which mentioned in MDN documentation:

disabled

Indicates whether the element is disabled or not. If this attribute is set, the element is disabled. Disabled elements are usually drawn with grayed-out text. If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event will not fire. In the case of form elements, it will not be submitted.

If you want to prevent user input but the existing value inside textbox will be sent together in form submit, use readonly attribute instead:

@Html.TextBoxFor(model => model.Username, new { @class = "form-control", @readonly = "readonly" })

Since readonly is part of C# keyword, you need to use @ prefix to escape it as HTML attribute. Note that you can stylize readonly element to look like disabled input by using CSS.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.