Код: Выделить всё
var calculatedBusinessDays =
1 + ((DateIssued - DateRequestMade).TotalDays * 5 -
(DateRequestMade.DayOfWeek - DateIssued.DayOfWeek) * 2) / 7;
if (DateIssued.DayOfWeek == DayOfWeek.Saturday)
calculatedBusinessDays--;
if (DateRequestMade.DayOfWeek == DayOfWeek.Sunday)
calculatedBusinessDays--;
BusinessDays = (int)calculatedBusinessDays;
ViewBag.Result = BusinessDays;
Код: Выделить всё
using System;
using System.Collections;
using System.Linq;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Office2010.Excel;
using FluentValidation;
using FluentValidation.AspNetCore;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Research.Data;
using ValidatorAttribute = ServiceStack.FluentValidation.Attributes.ValidatorAttribute;
using Research.Models;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
namespace Research.Controllers
{
public class LofAccessHCsController : Controller
{
private readonly ResearchContext _context;
public LofAccessHCsController(ResearchContext context)
{
_context = context;
}
// GET: LofAccessHCs
[HttpGet]
public async Task Index(
string sortOrder,
string currentFilter,
string searchString,
int? pageNumber)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["ProjIDSortParm"] = String.IsNullOrEmpty(sortOrder) ? "Proj_desc" : "";
if (searchString != null)
{
pageNumber = 1;
}
else
{
searchString = currentFilter;
}
ViewData["CurrentFilter"] = searchString;
var aField = from s in _context.LofAccessHC
select s;
if (!String.IsNullOrEmpty(searchString))
{
aField = aField.Where(s => s.ProjectID != null).Where(predicate: s => s.ProjectID.ToString().Contains(searchString));
}
switch (sortOrder)
{
case "Proj_desc":
aField = aField.OrderByDescending(s => s.ProjectID);
break;
default:
aField = aField.OrderBy(s => s.ProjectID);
break;
}
int pageSize = 100;
return View(await PaginatedList.CreateAsync(aField.AsNoTracking(), pageNumber ?? 1, pageSize));
}
}
// GET: LofAccessHCs/Create
[HttpGet]
public IActionResult Create( )
{
ViewData["ProjectID"] = new SelectList(_context.PIF, "ProjectID", "ProjectID");
var type = _context.LofAccessType!.ToList();
ViewBag.type = type;
var yn = _context.YesNoList!.ToList();
ViewBag.yn = yn;
return View();
}
// POST: LofAccessHCs/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create(
[Bind("LetterOfAccessHCID,ProjectID,DateRequestMade,RequestedBy,WithResearchPassport,DateIssued,DateofExpiry,IssuedTo,LoAType,businessDays")] LofAccessHC lofAccessHC, DateTime DateIssued, DateTime DateRequestMade, int BusinessDays,
string action,
string returnUrl)
{
LofAccessHCValidator lofaccesshcvalidator = new LofAccessHCValidator();
ValidationResult result = lofaccesshcvalidator.Validate(lofAccessHC);
// When validation failed, return to Index View
if (!ModelState.IsValid)
{
foreach (var failure in result.Errors)
{
ModelState.AddModelError(failure.PropertyName, failure.ErrorMessage);
}
ViewData["ProjectID"] = new SelectList(_context.PIF, "ProjectID", "ProjectID", lofAccessHC.ProjectID);
var type = _context.LofAccessType!.ToList();
ViewBag.type = type;
var yn = _context.YesNoList!.ToList();
ViewBag.yn = yn;
return View(lofAccessHC);
}
}
// Perform DB update after validation is passed
if (action == "SaveAndBack" && !String.IsNullOrEmpty(returnUrl))
{
try
{
_context.Add(lofAccessHC);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!LofAccessHCExists(lofAccessHC.LetterOfAccessHCID))
{
return NotFound();
}
else
{
throw;
}
}
var calculatedBusinessDays =
1 + ((DateIssued - DateRequestMade).TotalDays * 5 -
(DateRequestMade.DayOfWeek - DateIssued.DayOfWeek) * 2) / 7;
if (DateIssued.DayOfWeek == DayOfWeek.Saturday)
calculatedBusinessDays--;
if (DateRequestMade.DayOfWeek == DayOfWeek.Sunday)
calculatedBusinessDays--;
// Fix: Assign the calculated value to the outer scope variable 'BusinessDays' if needed.
BusinessDays = (int)calculatedBusinessDays;
// Ensure all code paths return a value
ViewBag.Result = BusinessDays;
// Ensure all code paths return a value
// Redirection after model validation passed and successfully update to database
var redirectUrl = Url.Action("Details", "PIFs", new { id = lofAccessHC.ProjectID });
if (string.IsNullOrEmpty(redirectUrl))
{
return NotFound(); // Handle the case where the URL could not be generated
}
return Redirect(redirectUrl);
}
else
{
try
{
_context.Add(lofAccessHC);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!LofAccessHCExists(lofAccessHC.LetterOfAccessHCID))
{
return NotFound();
}
else
{
throw;
}
}
var calculatedBusinessDays =
1 + ((DateIssued - DateRequestMade).TotalDays * 5 -
(DateRequestMade.DayOfWeek - DateIssued.DayOfWeek) * 2) / 7;
if (DateIssued.DayOfWeek == DayOfWeek.Saturday)
calculatedBusinessDays--;
if (DateRequestMade.DayOfWeek == DayOfWeek.Sunday)
calculatedBusinessDays--;
// Fix: Assign the calculated value to the outer scope variable 'BusinessDays' if needed.
BusinessDays = (int)calculatedBusinessDays;
// Ensure all code paths return a value
ViewBag.Result = BusinessDays;
return RedirectToAction(nameof(Index));
}
}
Код: Выделить всё
@Html.DisplayFor(modelItem => item.BusinessDays)
Подробнее здесь: https://stackoverflow.com/questions/797 ... ay-differe