1

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

人员:姓名、地址、地址 1、cityid、stateid、countryid、pincode、..
公司:姓名、地址、address1、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 以便自动映射器可以提取所有数据)

和 editortemplate 在 /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 视图中,我想调用地址模型的编辑器模板。

<%@ 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'类型的模型项”。
在这种情况下,我不知道如何将自动映射器生成的地址模型传递给编辑器模板。

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

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

4

1 回答 1

0

发现这篇文章有助于解决这个问题 - http://thatextramile.be/blog/2011/01/prefixing-input-elements-of-partial-views-with-asp-net-mvc/

于 2014-02-13T07:21:38.043 回答