请教一个,我设计了一个BS端的软件,用于工厂工时填写用,但遇到一个问题,在操作人员填写日产量和累计产量,比如是1.256时,保存总是自动的四舍五入成了1.260,请见附图,我们需要最大保留小数点后的三位,不要四舍五入 ,这两个字段的数据类型是decimal(18,3),请指导一下是什么原因导致的自动四舍五入吧;
相关图片如下:


相关代码如下
填写部分的设计(Index.cshtml)
<div class="col-6">
<label asp-for="Input.DailyOutput" class="form-label"></label>
<input asp-for="Input.DailyOutput" class="form-control" />
</div>
<div class="col-6">
<label asp-for="Input.CumulativeOutput" class="form-label"></label>
<input asp-for="Input.CumulativeOutput" class="form-control" />
</div>
保存部分代码(Index.cshtml.cs):
DailyOutput = Input.DailyOutput ?? 0,
CumulativeOutput = Input.CumulativeOutput ?? 0,
前端的完整代码;production.js
$(document).ready(function () {
// 标记用户是否手动修改过累计产量
var userModifiedCumulative = false;
// 当日产量自动填充累计产量(仅在用户未手动修改时)
$('#Input_DailyOutput').on('input', function () {
var daily = $(this).val();
if (!userModifiedCumulative) {
$('#Input_CumulativeOutput').val(daily);
}
});
// 监听累计产量输入,检测用户手动修改
$('#Input_CumulativeOutput').on('input', function () {
var val = $(this).val();
if (val === '') {
// 如果用户清空了累计产量,重置标记,允许重新自动同步
userModifiedCumulative = false;
} else {
userModifiedCumulative = true;
}
});
// 产品名称变更(静态元素)
$('#productNameSelect').change(function () {
var productName = $(this).val();
$('#specificationSelect').html('<option value="">-- 加载中... --</option>');
$('#processSelect').html('<option value="">-- 请先选择规格 --</option>');
$('#selectedProductId').val('');
$('#unitField').val('');
if (productName) {
$.getJSON(`?handler=Specifications&productName=${encodeURIComponent(productName)}`, function (data) {
var options = '<option value="">-- 请选择规格 --</option>';
$.each(data, function (i, item) {
options += `<option value="${item.value}">${item.text}</option>`;
});
$('#specificationSelect').html(options);
});
} else {
$('#specificationSelect').html('<option value="">-- 请选择产品名称 --</option>');
}
});
// 规格变更(动态元素)→ 使用委托
$(document).on('change', '#specificationSelect', function () {
var productId = $(this).val();
$('#processSelect').html('<option value="">-- 加载中... --</option>');
$('#unitField').val('');
if (productId) {
$.getJSON(`?handler=Processes&productId=${productId}`, function (data) {
var options = '<option value="">-- 请选择工序 --</option>';
$.each(data, function (i, item) {
options += `<option value="${item.value}">${item.text}</option>`;
});
$('#processSelect').html(options);
});
} else {
$('#processSelect').html('<option value="">-- 请先选择规格 --</option>');
$('#unitField').val('');
}
});
// 工序变更(动态元素)→ 使用委托
$(document).on('change', '#processSelect', function () {
var processId = $(this).val();
console.log('工序变更,processId=', processId); // 新增
if (processId) {
$.getJSON(`?handler=ProcessUnit&processId=${processId}`, function (data) {
console.log('获取到单位:', data.unit); // 添加此行
$('#unitField').val(data.unit);
});
} else {
$('#unitField').val('');
}
});
// 当日产量自动填充(静态元素)
$('#Input_DailyOutput').on('input', function () {
var daily = $(this).val();
var cumulative = $('#Input_CumulativeOutput').val();
if (cumulative === '' || parseFloat(cumulative) === 0) {
$('#Input_CumulativeOutput').val(daily);
}
});
// ========== 新增:计算人数和总工时 ==========
function updateTotals() {
var totalWorkers = 0;
var totalHours = 0;
$('#workerHourContainer .worker-row').each(function () {
var nameInput = $(this).find('input[name$=".EmployeeName"]');
var hoursInput = $(this).find('input.hours-input');
var name = nameInput.val();
var hours = parseFloat(hoursInput.val()) || 0;
if (name && name.trim() !== '') {
totalWorkers++;
totalHours += hours;
}
});
$('#workerCount').val(totalWorkers);
$('#totalHours').val(totalHours.toFixed(1));
}
// 监听姓名变化(动态元素)
$(document).on('input', 'input[name$=".EmployeeName"]', function () {
updateTotals();
});
// 监听工时变化(原有事件基础上添加 updateTotals)
$(document).on('input', '.hours-input', function () {
updateTotals();
});
// 添加人员行(静态按钮)
$('#addRowBtn').click(function () {
var index = $('#workerHourContainer .worker-row').length;
var newRow = `<div class="row g-2 mb-2 worker-row">
<div class="col-7">
<input type="text" name="Input.WorkerHours[${index}].EmployeeName" class="form-control" placeholder="人员姓名" />
</div>
<div class="col-4">
<input type="number" step="any" name="Input.WorkerHours[${index}].HoursWorked" class="form-control hours-input" placeholder="工时" />
</div>
<div class="col-1">
<button type="button" class="btn btn-outline-danger btn-sm remove-row">×</button>
</div>
</div>`;
$('#workerHourContainer').append(newRow);
});
// 删除人员行(动态元素)→ 使用委托
$(document).on('click', '.remove-row', function () {
$(this).closest('.worker-row').remove();
reindexWorkerRows();
});
// 重新索引所有行的name属性
function reindexWorkerRows() {
$('#workerHourContainer .worker-row').each(function (index) {
$(this).find('input[name^="Input.WorkerHours"]').each(function () {
var name = $(this).attr('name');
name = name.replace(/\[[0-9]+\]/, `[${index}]`);
$(this).attr('name', name);
});
});
}
});
模型设计如下:
public class ProductionEntryViewModel
{
// 顶部字段
[Required]
[DataType(DataType.Date)]
public DateTime ProductionDate { get; set; } = DateTime.Today;
// 其他手填字段0310添加
public int Id { get; set; }
[Display(Name = "产品名称")]
public int? SelectedProductId { get; set; } // 最终选中的具体产品Id(包含规格)
[Display(Name = "规格")]
public int? SelectedSpecificationProductId { get; set; } // 与SelectedProductId实际相同,用于联动
[Display(Name = "工序")]
public int? SelectedProcessId { get; set; }
// 其他手填字段
[Display(Name = "批号")]
public string BatchNo { get; set; }
[Display(Name = "产品批号")]
public string ProductBatchNo { get; set; }
[Display(Name = "单位")]
public string Unit { get; set; }
[Display(Name = "当日产量")]
public decimal? DailyOutput { get; set; }
[Display(Name = "累计产量")]
public decimal? CumulativeOutput { get; set; }
[Display(Name = "完工状态")]
public string CompletionStatus { get; set; } // 下拉选项
[Display(Name = "人数")]
public int NumberOfWorkers { get; set; }
[Display(Name = "总工时")]
public decimal ManHours { get; set; }
[Display(Name = "备注")]
[ValidateNever]
public string Remarks { get; set; }
// 人员详细工时列表(动态添加)
public List<WorkerHourEntry> WorkerHours { get; set; } = new List<WorkerHourEntry>();
// 用于联动下拉的数据源
[ValidateNever]
public List<SelectListItem> ProductNames { get; set; } // 去重的产品名称列表
[ValidateNever]
public List<SelectListItem> Specifications { get; set; } // 根据产品名称筛选出的规格(产品)
[ValidateNever]
public List<SelectListItem> Processes { get; set; } // 根据产品Id筛选出的工序
[ValidateNever]
public List<SelectListItem> CompletionStatuses { get; set; } // 完工状态预设
public string SelectedProductName { get; set; }
}
public class WorkerHourEntry
{
public string EmployeeName { get; set; }
public decimal HoursWorked { get; set; }
}