• 表单中的readOnly 和disabled


    readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容。但是它们之间有着微小的差别,总结如下:

    disabled也可以禁用按钮和链接:

    <button type="button" class="btn btn-primary btn-lg" disabled="disabled">
    禁用的原始按钮
    </button>

    <a href="#" class="btn btn-primary btn-lg disabled" role="button">
    禁用的原始链接
    </a>

    Readonly只针对input(text / password)和textarea有效,而disabled对于所有的表单元素都有效,包括select, radio, checkbox, button等。

    但是表单元素在使用了disabled后,当我们将表单以POST或GET的方式提交的话,这个元素的值不会被传递出去,而readonly会将该值传递出去(这种情况出现在我们将某个表单中的textarea元素设置为disabled或readonly,但是submit button却是可以使用的)。

    在asp.net mvc 的视图中要显示某个表单值而不更改其值,并且处理表单的操作方法中要调用这个值,在视图中有三种方法处理:

    1、将该值作为隐藏字段处理,而在显示中用displayFor HTML辅助方法,字段值就作为一个文本处理,当然表单提交也不会提交该字段。

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

    <div class="form-horizontal">

    <hr />
    @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、将该值作为隐藏字段处理,在Editor Html辅助方法中添加disabled="disabled" html属性值,这样就显示一个灰色的文本框。因为表单已经被disabled掉,所以表单不会提交该字段值到控制器的操作方法中。

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

    <div class="form-horizontal">

    <hr />
    @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、使用 readonly属性,也会显示一个灰色的文本框。由于readonly 是C#的关键字。所有只能使用原始的HTML标记。这种情况表单字段值会提交到控制器的操作方法,要注意手动设置表单字段的Name 属性和value属性。

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

    <div class="form-horizontal">

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

    相关的action值:

    [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();


    }

  • 相关阅读:
    C# 事务之SqlTransaction
    java获取字符串格式日期向前或向后n天的日期
    java中保留几位小数
    第一个androidAPP项目总结—ListView的上拉和下拉
    java中static作用详解
    第一个androidAPP项目总结—数据请求
    获取控件的高和宽
    Android学习笔记:如何设置ImageView中图片的显示方式
    TextView过长显示省略号, TextView文字中间加横线
    android中控件的使用
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4455775.html
Copyright © 2020-2023  润新知