Asp.net mvc MVC Post值为空
我一直在寻找这一点,但我似乎找不到它为什么会发生的答案。我认为,如果我能理解为什么会发生这种情况,我可以在将来防止它 所以我有一个基本的编辑视图,看起来像:Asp.net mvc MVC Post值为空,asp.net-mvc,post,Asp.net Mvc,Post,我一直在寻找这一点,但我似乎找不到它为什么会发生的答案。我认为,如果我能理解为什么会发生这种情况,我可以在将来防止它 所以我有一个基本的编辑视图,看起来像: @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>EditUserViewModel</h4> <hr />
@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>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
EditUserViewModel
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Username,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.TextBoxFor(model=>model.Username,新的{@class=“form control”,disabled=“disabled”})
@Html.ValidationMessageFor(model=>model.Username,“,new{@class=“text danger”})
@LabelFor(model=>model.FirstName,“FirstName”,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.LastName,“LastName”,htmlAttributes:new{@class=“control-label col-md-2”})
@EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”})
@LabelFor(model=>model.PhoneNumber,“电话号码”,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PhoneNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PhoneNumber,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
@LabelFor(model=>model.Role,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownListFor(model=>model.Role,new SelectList(@model.Roles,model.Role),new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Role,“,new{@class=“text danger”})
@Html.Partial(“_SaveButtons”,新的SaveButtonsViewModel(“Admin”,“Index”,“Index”))
}
请注意,我在创建视图后添加了有问题的项(用户名)
因此,当我点击“保存”并发送帖子时,用户名总是空的。我开始研究它,我注意到它不在表单数据中:
因此,现在我的问题是,当它位于表单内部时,为什么不将其添加到表单中,并且我使用@Html helper方法?任何禁用了
属性的输入元素都不会作为表单数据的一部分提交,如中所述:
已禁用
指示元素是否已禁用。如果此属性为
设置时,元素被禁用。禁用的图元通常使用
灰色文本。如果元素被禁用,则它不会响应
用户操作,它不能被聚焦,命令事件也不会被聚焦
开火如果是表单元素,则不会提交。
如果要阻止用户输入,但文本框中的现有值将在表单提交中一起发送,请改用readonly
属性:
@Html.TextBoxFor(model => model.Username, new { @class = "form-control", @readonly = "readonly" })
由于readonly
是的一部分,因此需要使用@
前缀将其作为HTML属性转义。请注意,您可以使用CSS将只读元素样式化为禁用的输入。1。因为它被禁用了,所以它不是表单数据的一部分。2.您真的希望您的客户机确定用户的角色吗?1,因为它被禁用,所以它不会成为表单的一部分?2:我确实希望管理员能够编辑用户的角色,为什么我不想要呢?1。对2.在这种情况下,我假设它是好的。禁用了属性的输入元素不会作为表单数据发送,因此您应该使用只读:Html.TextBoxFor(model=>model.Username,new{@class=“form control”,@readonly=“readonly”}
。