如何在MVC 5中向modal添加自定义字段/属性

时间:2016-03-09 05:58:52

标签: c# asp.net-mvc model

我正在从数据库查询计算总字段,但它不是模型属性或数据库字段。查询执行后,如何绑定该总模型,因为我必须返回要在表中显示的模型列表。 提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以使用Viewmodel。每次你必须选择使用ViewModel.Because一段时间后你需要concat模型或验证消息。 我添加了基本的示例视图模型。

public class EmployeeViewModel 
{
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First name required")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Last name required")]
    public string LastName { get; set; }
    public decimal PersonelSalary {get;set;}
}

使用此视图模型,您可以定义视图(Razor视图引擎):

@model SolutionName.Models.ViewModels.EmployeeViewModel