【问题标题】:When I add new row in the table, how can I set default value for all the decimal column in the new当我在表中添加新行时,如何为新中的所有小数列设置默认值
【发布时间】:2021-08-24 15:12:51
【问题描述】:

我在一行中有一些小数列,它们分组在 emphrs 类下。但是当我在表中添加新行时,如何将 0.00 设置为 emphrs 类下分组的所有列的默认值

在我的模型类 EmpHrs.cs 中的列属性

 public decimal SundayNetHrs { get; set; }
  public decimal MondayNetHrs { get; set; }
  public decimal TuesdayNetHrs { get; set; }
  public decimal WednesdayNetHrs { get; set; }
  public decimal ThursdayNetHrs { get; set; }
  public decimal FridayNetHrs { get; set; }
  public decimal SaturdayNetHrs { get; set; }

在 EmpHrs Html 我有一个带有 EmpHrs 列的表

<tbody>
        <tr>
            <td>@Html.TextBoxFor(model => model.FromDate, new { @class = "form-control date-input" })</td>
            <td>@Html.TextBoxFor(model => model.ToDate, new { @class = "form-control date-input" })</td>
            <td>@Html.EditorFor(model => model.SundayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
            <td>@Html.EditorFor(model => model.MondayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
            <td>@Html.EditorFor(model => model.TuesdayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
            <td>@Html.EditorFor(model => model.WednesdayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>

       </tr>
     </tbody>
  <button type="button" id="btnUpdate" name="submitButton" value="DeleteAtt"
                 class="btn btn-primary form-control" onclick="AddNew();">
                 <i class="fa fa-save"></i><span class="padding-left-ten">Add New Row</span>
 </button>

 function AddNew() {
        var clone = $("#tblEntry tr:last").clone().find('input').val('').end().insertAfter("#tblEntry tr:last");
        $(clone).find(".date-input").datepicker({ format: 'dd/mm/yyyy', todayHighlight: true, date: new Date(), autoclose: true, todayBtn: 'linked' });
       // Here  All column under EmpHrs class should be set default value 0.00
    }

【问题讨论】:

    标签: html asp.net-core


    【解决方案1】:

    您可以尝试以下方式:

    型号:

    public class EmpHrs
        {
            [DataType(DataType.Date, ErrorMessage = "Date only")]
            [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
            public DateTime FromDate { get; set; }
    
            [DataType(DataType.Date, ErrorMessage = "Date only")]
            [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
            public DateTime ToDate { get; set; }
            public decimal SundayNetHrs { get; set; }
            public decimal MondayNetHrs { get; set; }
            public decimal TuesdayNetHrs { get; set; }
            public decimal WednesdayNetHrs { get; set; }
            public decimal ThursdayNetHrs { get; set; }
            public decimal FridayNetHrs { get; set; }
            public decimal SaturdayNetHrs { get; set; }
        }
    

    控制器:

     public IActionResult SetDefaultValue()
            {
         
                var empHrs=    new EmpHrs() { SundayNetHrs = 10, MondayNetHrs = 10, TuesdayNetHrs = 2, WednesdayNetHrs = 2, ThursdayNetHrs = 3, FridayNetHrs = 5, SaturdayNetHrs = 9 };
                return View(empHrs);
            }
    

    注意:我在加载时绑定了一些默认值。如果需要,可以跳过它。单击时,我们将使用javascript 设置默认值 以及下一部分。

    查看:

    @model EmpHrs
    @{
        ViewData["Title"] = "SetDefaultValue";
    }
    <table id="tblEntry" class="table table-striped">
        <thead>
            <tr>
                <th>Date From</th>
                <th>Date To</th>
                <th>SundayNetHrs</th>
                <th>MondayNetHrs</th>
                <th>TuesdayNetHrs</th>
                <th>WednesdayNetHrs</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>@Html.EditorFor(model => model.FromDate, new { @id = "FromDate", htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
                <td>@Html.EditorFor(model => model.ToDate, new { @id = "ToDate", htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
                <td>@Html.EditorFor(model => model.SundayNetHrs, new {@id = "SundayNetHrs", htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
                <td>@Html.EditorFor(model => model.MondayNetHrs, new { @id = "MondayNetHrs", htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
                <td>@Html.EditorFor(model => model.TuesdayNetHrs, new { @id = "TuesdayNetHrs", htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
                <td>@Html.EditorFor(model => model.WednesdayNetHrs, new { @id = "WednesdayNetHrs", htmlAttributes = new { type = "number", @class = "form-control  w-100 empHrs" } })</td>
    
            </tr>
        </tbody>
    </table> <div class="row padding-top-ten center-block">
        <div class="col-md-4">
        </div>
        <div class="col-md-6">
            <div class="col-sm-4">
                <button type="button" id="btnUpdate" name="submitButton" value="DeleteAtt"
                        class="btn btn-primary form-control" onclick="AddNew();">
                    <i class="fa fa-save"></i><span class="padding-left-ten">Add New Row</span>
                </button>
            </div>
        </div>
    </div>
    

    脚本:

    <script>
        function AddNew() {
           // alert("Click");
            var newRow = $("#tblEntry tbody tr").first().clone();
            var row = $("tbody").append(newRow);
            //Set Default value while adding new rows
             var defaultValue = 0;
       
        $("tbody tr:last-of-type td").find("input[id*='SundayNetHrs']").val(defaultValue.toFixed(2));
        $("tbody tr:last-of-type td").find("input[id*='MondayNetHrs']").val(defaultValue.toFixed(2));
        $("tbody tr:last-of-type td").find("input[id*='TuesdayNetHrs']").val(defaultValue.toFixed(2));
        $("tbody tr:last-of-type td").find("input[id*='WednesdayNetHrs']").val(defaultValue.toFixed(2));
          
           
        }
    </script>
    

    注意: 看看这里的tbody tr:last-of-type td 选择器,我会找到每个inputbox 并为其分配一个值。所以只要找到 您期望的输入项并使用defaultValuetoFixed javascript 方法来设置十进制指针。

    输出:

    希望它能解决您的问题。

    【讨论】:

      【解决方案2】:

      使用具体值:

        public decimal SundayNetHrs { get; set; }=0.00;
        public decimal MondayNetHrs { get; set; }=0.00;
        public decimal TuesdayNetHrs { get; set; }=0.00;
        public decimal WednesdayNetHrs { get; set; }=0.00;
        public decimal ThursdayNetHrs { get; set; }=0.00;
        public decimal FridayNetHrs { get; set; }=0.00;
        public decimal SaturdayNetHrs { get; set; }=0.00;
      

      【讨论】:

        猜你喜欢
        • 2017-08-31
        • 1970-01-01
        • 2020-09-01
        • 2011-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        相关资源
        最近更新 更多