User-1355965324 posted
The value of NetHrs is not passing into Model variable from the javascript function. My model class has the following variable. The net Hrs is showing there on the html, but the value is not available in the controller model variable
public class BreakDownLogEntryModel
{
public decimal NetHrs;
}
Controller
public IActionResult BreakDownLogEntry(BreakDownLogEntryModel BreakDownLog)
{
if (HttpContext.Session.GetInt32("UserID") != null)
{
if (BreakDownLog.NetHrs == 0) // Here the value is not coming . BreakDownLog.NetHrs is always zero. Please can you help
{
ViewBag.Error = "Hrs must be given";
}
}
}
Html file
<div class="row">
<div class="form-group">
<label for="lblTJS" class="control-label col-sm-1">Time Job Start</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="TimeStart" asp-for="TimeJobStart" onchange="CalculateBreakDownRemainingHrs()"
onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||event.charCode == 46 || event.charCode == 0 ">
</div>
<label for="lblTJF" class="control-label col-sm-1">Time Job Finish</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="TimeEnd" asp-for="TimeJobFinish" onchange="CalculateBreakDownRemainingHrs()"
onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||event.charCode == 46 || event.charCode == 0 ">
</div>
<label for="lblTJF" class="control-label col-sm-1">Hrs</label>
<div class="col-sm-2">
<input type="text" class="form-control" asp-for="NetHrs" id="NetHrs" >
</div>
</div>
</div>
function CalculateBreakDownRemainingHrs() {
var fromTime = $('#TimeStart').val().replace('.', ':');
var toTime = $('#TimeEnd').val().replace('.', ':');
var fromdate = new Date("05/25/2018 " + fromTime + ":00");
var todate = new Date("05/25/2018 " + toTime + ":00");
var msec = todate.getTime() - fromdate.getTime();
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
netHrs = hh + "." + mm;
$("#NetHrs").val(netHrs);
}