1

データベースに住所フィールドを持つ複数のテーブルがあります。元:

個人: 名前、住所、住所 1、都市 ID、州 ID、国 ID、ピンコード、..
会社: 名前、住所、住所 1、都市 ID、州 ID、国 ID、ピンコード、..
..

関連するビューモデル:

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;}
}

(注: automapper がすべてのデータを取り込めるように、personmodel で AddressModel を使用しませんでした)

/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:
テンプレートは、フィールド アクセス、プロパティ アクセス、単一次元配列インデックス、または単一パラメーターのカスタム インデクサー式でのみ使用できます。

を使用したいのですが、 「System.InvalidOperationException: ディクショナリに渡されたモデル項目は 'CustomerModel' 型ですが、このディクショナリには 'AddressModel' 型のモデル項目が必要です」Html.EditorForModel("AddressModel");というエラーがスローされます。 この場合、オートマッパーが生成したアドレスモデルをエディターテンプレートに渡す方法がわかりません。

この場合、アドレス フィールドに basicInfo をプレフィックスとして付けたいため、partialViews を使用できません。別のケースではプレフィックスは必要ありません。

これは私を数日間夢中にさせています。助けてください!!!

4

1 に答える 1