简体   繁体   English

ASP.NET MVC 5 Razor视图映射复选框到日期字段

[英]ASP.NET MVC 5 Razor view map checkbox to a date field

I am relatively new to ASP.NET MVC. 我对ASP.NET MVC比较陌生。 I have an MVC 5 application that's displaying results to a DataTable using the DataTable.net library. 我有一个MVC 5应用程序,该应用程序使用DataTable.net库将结果显示到DataTable中。 The field I'm trying to modify is a Date field but it's converted to a string by the View. 我尝试修改的字段是“日期”字段,但已通过“视图”将其转换为字符串。 What I'd like to do is add a checkbox to the left of the date string and if the date has a value in it then make the box checked. 我想做的是在日期字符串的左侧添加一个复选框,如果日期中有一个值,则选中该复选框。 If it doesn't have a value then leave it unchecked. 如果它没有值,请不要选中它。 This is without adding a boolean field to the table. 这没有在表中添加布尔字段。 Just strictly reading if the field is null and toggling the checkbox based off of checking if it's null. 只需严格阅读该字段是否为空,然后基于检查该字段是否为空来切换复选框即可。 Then the user can select multiple records and pass the selected records to the controller and update the database. 然后,用户可以选择多个记录,并将选定的记录传递给控制器​​并更新数据库。 I've tried doing a Checkboxfor but it complains about not being able to convert a system.date to a boolean. 我尝试过使用Checkboxfor,但是它抱怨无法将system.date转换为布尔值。 Below is the section of code for the view that populates the Table: 以下是填充表格的视图的代码部分:

<table id="SampleLogTable" class="table table-hover">
<thead>
    <tr>
        <th>@Html.LabelFor(m => m.Data.FirstOrDefault().Id)</th>
        <th>@Html.LabelFor(m => m.Data.FirstOrDefault().FPSampleType.Name</th>
        <th class="hidden-xs">@Html.LabelFor(m =>m.Data.FirstOrDefault().FPLotNumber.Value)</th>
        <th>@Html.LabelFor(m => m.Data.FirstOrDefault().FPProgram.Name)</th>
        <th>@Html.LabelFor(m => m.Data.FirstOrDefault().FPQualityControl.Value)</th>
        <th class="hidden-xs">@Html.LabelFor(m => m.Data.FirstOrDefault().Comments)</th>
        <th class="date-col">@Html.LabelFor(m => m.Data.FirstOrDefault().DateAssigned)</th>
        <th class="date-col">@Html.LabelFor(m => m.Data.FirstOrDefault().CheckedInDate)</th>
    <tr>
</thead>
<tbody>
    @foreach (var log in Model.Data)
    {
        <tr data-record-id="@log.Id">
            <td>@log.Id</td>
            <td>@log.FPSampleType.Name</td>
            <td class="hidden-xs">@(log.FPLotNumber == null ? "" : log.FPLotNumber.Value)</td>
            <td>@log.FPProgram.Name</td>
            <td>@(log.FPQualityControl == null ? "" : log.FPQualityControl.Value)</td>
            <td class="hidden-xs">@(Html.StringPreview(log.Comments, 20))</td>            
            <td>@log.DateAssigned.ToShortDateString()</td>
            <td> @(log.CheckedInDate == null ? "" : log.CheckedInDate.Value.ToShortDateString())</td>

   @*other code not related to the data*@
      .....
</tbody>

I'm not sure I fully understand your question, I hope this helps. 我不确定我是否完全理解您的问题,希望对您有所帮助。

What I'd like to do is add a checkbox to the left of the date string and if the date has a value in it then make the box checked: 我想做的是在日期字符串的左侧添加一个复选框,如果日期中有一个值,则选中该复选框:

<td>@Html.CheckBox("name", log.CheckedInDate == null ? false: true)
     @(log.CheckedInDate == null ? "" : log.CheckedInDate.Value.ToShortDateString())</td>

well i have a different solution but working i believe 好吧,我有一个不同的解决方案,但我相信我的工作

   @foreach (var log in Model.Data)
        {
            <tr data-record-id="@log.Id">
                <td>@log.Id</td>
                <td>@log.FPSampleType.Name</td>
                <td class="hidden-xs">@(log.FPLotNumber == null ? "" : 
                 log.FPLotNumber.Value)</td>
                <td>@log.FPProgram.Name</td>
                <td>@(log.FPQualityControl == null ? "" : log.FPQualityControl.Value)</td>
                <td class="hidden-xs">@(Html.StringPreview(log.Comments, 20))</td>            
                <td>@log.DateAssigned.ToShortDateString()</td>
          @{
            if(logCheckedInDate.CheckedInDate==null )
              {
              <td></td>
              <td><input class="form-control" type="checkbox"></td>
              }
              else
              {
                <td>log.CheckedInDate.Value.ToShortDateString()</td>
                <td><input class="form-control" type="checkbox" checked></td>
              }        
           } 
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM