我有多个表,这些表在我的数据库中都有地址字段。前任:
人员:姓名,地址,地址1,cityid,stateid,countryid,pincode等..
公司:姓名,地址,地址1,stateid,cityid,stateid,countryid,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;}
}
我为地址创建了一个类:
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中的相同的editortemplate
<%@ 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视图中,我想调用地址模型的编辑器模板。
<%@ 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.EditorForModel("AddressModel");
,但是抛出一个错误
“ System.InvalidOperationException:传递到字典中的模型项的类型为'CustomerModel',但是此字典需要类型为'AddressModel'的模型项”。
在这种情况下,我不知道如何将自动映射器生成的地址模型传递给editortemplate。
我不能使用partialViews,因为在这种情况下我希望地址字段以basicInfo为前缀,而在另一种情况下则不需要任何前缀。
这让我发疯了几天。请帮忙!!!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句