我正在尝试创建一个允许客户添加到数据库的Intranet,然后允许添加有关它们的更多信息。主要问题是,对于像员工信息这样的事情,有多个员工,所以我尝试将客户信息模型放在AddCustomers模型详细信息页面中,但是这条消息被返回了。
分析器错误
描述:解析资源期间发生错误 需要为此请求提供服务。请查看以下具体内容 解析错误详细信息并适当修改源文件。
分析程序错误消息:文件中只允许使用一个“模型”语句。
来源错误:
Line 36: Line 37: <!--------------------------------------------------------------------------> Line 38: @model IEnumerable<Intranet.Models.EmployeeInfo> Line 39: Line 40: @{
源文件:/Views/AddCustomers/Details.cshtml行:38
有没有办法绕过文件中只允许一个模型语句,或者有更好的方法来实现我想要做的事情吗?
编辑:
namespace Intranet.Models
{
public class AddCustomers
{
public int AddCustomersID { get; set; }
public string CompanyName { get; set; }
public string Status { get; set; }
}
public class EmployeeInfo
{
public int EmployeeInfoID { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string Title { get; set; }
public string Mobile { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
}
public class ContactInfo
{
public int ContactInfoID { get; set; }
public string Code { get; set; }
public string Address { get; set; }
public string Postcode { get; set; }
public string Telephone { get; set; }
}
我希望能够添加客户信息,例如在EmployeeInfo
创建客户后ContactInfo
和AddCustomers
。但是,由于有多个员工,我需要为信息提供单独的表,并希望在AddCustomers
详细信息视图中包含这些表/模型。
答案 0 :(得分:0)
正如Stephen所说,你需要一个ViewModel,它应该是View(AddCustomer)所需的一切。所以从你的描述中你可以使用类似的东西:
public class AddCustomerViewModel
{
public AddCustomerViewModel()
{
Employees = new List<EmployeeInfo>();
ContactInfo = new ContactInfo;
}
public int AddCustomersID { get; set; }
public string CompanyName { get; set; }
public string Status { get; set; }
// Contact info - If there is only 1, you could just add the properties to the ViewModel
public ContactInfo ContactInfo { get; set; }
// Related employee:
public List<EmployeeInfo> Employees { get; set; }
}
您可以在控制器的AddCustomer [GET]中填充此ViewModel。那么你的视图将引用视图模型:
@model AddCustomerViewModel
对于员工信息,因为它是一个集合,您将使用foreach循环:
// build table and header
@foreach (var employee in Model.Employees)
{
// kick out a <tr><td>...</td></tr> for each employee
}
对于CRUD部分,您需要添加表格和控制器POST,如演示中所示。