LINQ多表连接(MVC C#)代码错误排查及解决方案求助
Hey there! Let's dig into potential issues in your ViewModel code and work through fixes step by step.
1. 数值型属性误用string类型
Your LeaveBalance and PreviousBalance properties (which represent numeric values) are currently defined as string—this creates unnecessary headaches:
- Frontend display/ calculations will break (you can't easily add/subtract string values)
- Backend processing requires extra type conversions, which risk throwing exceptions
- You can't use data annotations to validate numeric ranges (like preventing negative balances)
Fix: Switch these properties to a numeric type like decimal (for fractional balances) or int (for whole-day leave):
public class LeaveInitialBalanceViewModelKendoGrid { [HiddenKendoGridColumn] public string ID { get; set; } public string EmployeeCode { get; set; } public string EmployeeName { get; set; } public string Department { get; set; } public string Section { get; set; } public string LeaveType { get; set; } // Use decimal for balances that might include fractions public decimal LeaveBalance { get; set; } public decimal PreviousBalance { get; set; } // Or use int if leave is tracked in whole days only // public int LeaveBalance { get; set; } }
2. Missing Data Annotations (Optional but Highly Recommended)
If this ViewModel is used for form submissions with Kendo Grid, add data annotations to enforce validation and improve UX:
using System.ComponentModel.DataAnnotations; public class LeaveInitialBalanceViewModelKendoGrid { [HiddenKendoGridColumn] public string ID { get; set; } [Required(ErrorMessage = "Employee code is required")] [MaxLength(20)] public string EmployeeCode { get; set; } [Required(ErrorMessage = "Employee name is required")] [MaxLength(50)] public string EmployeeName { get; set; } public string Department { get; set; } public string Section { get; set; } [Required(ErrorMessage = "Leave type is required")] public string LeaveType { get; set; } [Range(0, double.MaxValue, ErrorMessage = "Balance cannot be negative")] public decimal LeaveBalance { get; set; } [Range(0, double.MaxValue, ErrorMessage = "Previous balance cannot be negative")] public decimal PreviousBalance { get; set; } }
3. Truncated Code Cleanup
Your code cuts off at PreviousBalance { get;...}—make sure to:
- Finish defining all properties with complete get/set accessors (use
get; init;for read-only properties if needed) - Close all curly braces and fix any syntax errors from the truncated section
Quick Side Note
Double-check that your custom [HiddenKendoGridColumn] attribute is properly implemented and recognized by the Kendo Grid framework—if not, the ID column might still render on the frontend.
内容的提问来源于stack exchange,提问作者Søśø ÐålợȜà ZåȜră

