Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将使用automapper生成的模型传递到Html.editorformodel()中_C#_Asp.net Mvc_Editortemplates - Fatal编程技术网

C# 将使用automapper生成的模型传递到Html.editorformodel()中

C# 将使用automapper生成的模型传递到Html.editorformodel()中,c#,asp.net-mvc,editortemplates,C#,Asp.net Mvc,Editortemplates,我的数据库中有多个具有地址字段的表。 例: 人员:姓名、地址、地址1、城市ID、州ID、国家ID、pincode、。。 公司名称、地址、地址1、城市ID、州ID、国家ID、pincode、。。 相关视图模型: public class customermodel{ public PersonModel basicInfo {get;set;} public string type {get;set;} public long id {get;set;} ...

我的数据库中有多个具有地址字段的表。 例:

人员:姓名、地址、地址1、城市ID、州ID、国家ID、pincode、。。 公司名称、地址、地址1、城市ID、州ID、国家ID、pincode、。。

相关视图模型:

public class customermodel{
    public PersonModel basicInfo {get;set;}
    public string type {get;set;}
    public long id {get;set;}
    ...
}
public class PersonModel{
    public string FirstName {get;set;}
    public string MiddleName {get;set;}
    public string LastName {get;set;}
    public string Email {get;set;}
    public long Phone {get;set;}
    public string address {get;set;}
    public string address1 {get;set;}
    public long cityid {get;set;}
    public long stateid {get;set;}
    public long countryid{get;set;}
    public long pincode {get;set;}
}
我为address创建了一个类:

public class AddressModel{
    public string address {get;set;}
    public string address1 {get;set;}
    public long cityid {get;set;}
    public long stateid {get;set;}
    public long countryid{get;set;}
    public long pincode {get;set;}
}
注意:我没有在personmodel中使用AddressModel,以便automapper可以提取所有数据

在/Views/Shared/EditorTemplates/AddressModel.ascx中为相同的

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AddressModel>" %>

<%: Html.TextBoxFor(model => model.address, new { Placeholder = "Country" })%>
<%: Html.TextBoxFor(model => model.address1, new { Placeholder = "State", style="display:none;" })%>
...
从EditCustomer视图中,我想调用address模型的编辑器模板

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CustomerModel>" %>

<%: Html.TextBoxFor(model => model.id) %>
<%: Html.TextBoxFor(model => model.type) %>
<%: Html.EditorFor(model => (AddressModel)AutoMapper.Mapper.DynamicMap<AddressModel>(model.personModel), "AddressModel")%>
...
现在,我得到EditorFor行的以下错误: 模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式

我想使用Html.EditorForModelAddressModel;,但这让我犯了一个错误 System.InvalidOperationException:传递到字典的模型项的类型为“CustomerModel”,但此字典需要“AddressModel”类型的模型项。 在这种情况下,我不知道如何将automapper生成的addressmodel传递给editortemplate

我不能使用partialViews,因为在这种情况下,我希望地址字段以basicInfo作为前缀,而在另一种情况下,我不需要任何前缀


这让我疯狂了好几天。请帮忙

找到了这篇有助于解决此问题的文章-

成功了!我将PersonModel更改为public string LastName{get;set;}public string Email{get;set;}public long Phone{get;set;}public AddressModel AddressModel{get;set;}