MVC Post Value Null

时间:2018-08-22 14:00:53

标签: asp.net-mvc post

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?

1 个答案:

答案 0 :(得分:1)

任何启用了input属性的disabled元素都不会作为表单数据的一部分提交,MDN documentation中提到:

  

disabled

     

指示元素是否被禁用。如果此属性是   设置后,该元素被禁用。禁用元素通常使用   变灰的文字。如果该元素被禁用,则不会响应   用户操作,它无法集中,并且命令事件不会   火。 对于表单元素,将不会提交。

如果要阻止用户输入,但文本框内的现有值将以表单提交的形式一起发送,请改用readonly属性:

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

由于readonlyC# keyword的一部分,因此您需要使用@前缀将其转义为HTML属性。请注意,您可以使用CSS将readonly元素样式化为禁用输入。