ReadOnly and disabled in the form

Source: Internet
Author: User

ReadOnly and disabled are the two attributes that are used in the form, and they all enable users to not change the content in the form fields. But there is a slight difference between them, summed up as follows:

ReadOnly is valid only for input (Text/password) and textarea, and disabled is valid for all form elements, including Select, Radio, CheckBox, Button, and so on.

However, when the form element is used with disabled, when we submit the form as a post or get, the value of this element is not passed out. and ReadOnly will pass the value out (this happens when we set the TEXTAREA element in a form to Disabled or readonly, but the Submit button is available).

In an ASP. NET MVC view, to display a form value without changing its value, and to call this value in the action method for handling the form, there are three ways to handle it in the view:

1, the value as a hidden field processing, and in the display with the Displayfor HTML helper method, the field value as a text processing, of course, the form submission will not submit the field.

@using (Html.BeginForm ())
{
@Html. AntiForgeryToken ()

<div class= "Form-horizontal" >

@Html. ValidationSummary (True, "", new {@class = "Text-danger"})

@Html. Hiddenfor (Model =>model. UserName)
<div class= "Form-group" >
@Html. labelfor (model = model. UserName, htmlattributes:new {@class = "Control-label col-md-2"})
<div class= "Col-md-10" >
<p class= "Form-control-static" >
@Html. displayfor (model = model. UserName, new {htmlattributes = new {@class = "Form-control"}})
</p>

</div>
</div>

2, the value as a hidden field processing, in the Editor HTML helper method add disabled= "disabled" HTML property value, so that a gray text box is displayed. Because the form has been disabled out, the form does not submit the field value to the Controller's action method.

@using (Html.BeginForm ())
{
@Html. AntiForgeryToken ()

<div class= "Form-horizontal" >

@Html. ValidationSummary (True, "", new {@class = "Text-danger"})

@Html. Hiddenfor (Model =>model. UserName)
<div class= "Form-group" >
@Html. labelfor (model = model. UserName, htmlattributes:new {@class = "Control-label col-md-2"})
<div class= "Col-md-10" >
<p class= "Form-control-static" >
@Html. editfor (model = model. UserName, new {htmlattributes = new {@class = "Form-control", disabled= "Disabled"}})
</p>

</div>
</div>

3. Use the ReadOnly attribute, because ReadOnly is a C # keyword. All can only use the original HTML markup. In this case, the form field values are submitted to the Controller's action method, and note that the Name property and the Value property of the form field are manually set.

@using (Html.BeginForm ())
{
@Html. AntiForgeryToken ()

<div class= "Form-horizontal" >

@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" >
<input type= "text" value= "@Model. UserName" class= "Form-control" readonly= "readonly" name= "UserName"/>
</div>
</div>

The associated Action value:

[HttpPost]
[Validateantiforgerytoken]
Public ActionResult Changeuserpassword (Resetpasswordviewmodel _resetpasswordviewmodel)
{
if (! Modelstate.isvalid)
{
Return View (_resetpasswordviewmodel);
}

var _user = Usermanager.findbyname (_resetpasswordviewmodel.username);
if (_user = = null)
return new Httpstatuscoderesult (httpstatuscode.badrequest);

var code =usermanager.generatepasswordresettoken (_user. ID);
var result =usermanager.resetpassword (_user. Id,code,_resetpasswordviewmodel.password);
if (result. SUCCEEDED)
{
Return redirecttoaction ("Index");

}
return View ();


}

ReadOnly and disabled in the form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.