Lord Vermillion Lord Vermillion - 3 months ago 14
C# Question

TextBoxFor value not passed to model when disabled

I have a disabled input-field displaying data from a model.

<div class="form-group">
@Html.LabelFor(model => model.first_name, "Förnamn", new
{
@for = "inputFirstname",
@class = "col-lg-3 control-label"
})
<div class="col-lg-9">
@Html.TextBoxFor(model => model.first_name, new
{
@type = "text",
@class = "form-control",
@id = "inputFirstname",
text = Html.DisplayNameFor(model => model.first_name),
disabled="disabled"
})
</div>
</div>


I can submit this data to a controlelr method:

[HttpPost]

public ActionResult Index(RegistrationModel RegistrationModelViewModel)
{}


When i add
disabled="disabled"
the first_name data is null, if i remove it i get the correct data.

What am i doing wrong?

Answer

You may want to use readonly property, if you want to also display the data:

@Html.TextBoxFor(model => model.first_name, new
    {
        @readonly = "readonly"
    })