1

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109
  • 1
    Ask yourself why you'd expect data from a disabled field to passed to the model in the first place? – DGibbs Oct 27 '15 at 12:32
  • Possible duplicate of [How do I submit disabled input in ASP.NET MVC?](http://stackoverflow.com/questions/2700696/how-do-i-submit-disabled-input-in-asp-net-mvc) – Perttu Haliseva Oct 27 '15 at 12:37

2 Answers2

6

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

@Html.TextBoxFor(model => model.first_name, new
    {
        @readonly = "readonly"
    })
Perttu Haliseva
  • 530
  • 5
  • 16
2

You need to add an <input type="hidden" name="whateverName" /> on the page which matches the disabled field. By default, it will not be sent to the server.

Eric Hotinger
  • 8,957
  • 5
  • 36
  • 43