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

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

user7569
1#
user7569 Published in September 20, 2018, 12:43 pm

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>
share|improve this question
  • Is there a specific reason why you can't separate the two? – cloudstrifebro Feb 12 at 19:05
  • @cloudstrifebro doesn't the checkbox have to bind to a data field? I could separate them but how would the checkbox know which ID to pass or if the date field is null? – Braden Feb 12 at 19:17

2 Answers 2

active oldest votes
up vote 0 down vote accepted
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO