Home ASP.NET MVC 5 Razor view map checkbox to a date field
Reply: 2

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

Braden
1#
Braden Published in 2018-02-12 19:01:01Z

I am relatively new to ASP.NET MVC. I have an MVC 5 application that's displaying results to a DataTable using the DataTable.net library. 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. 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>
Kanstantsin Zinkevich
2#
Kanstantsin Zinkevich Reply to 2018-02-12 19:31:04Z

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>
RAHUL S R
3#
RAHUL S R Reply to 2018-02-12 19:53:02Z

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>
              }        
           } 
       }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305117 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO