Reputation: 1017
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:
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?
Upvotes: 0
Views: 46
Reputation: 24957
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.
Upvotes: 1