1

I am creating a partial view to display a record's audit stamps...i.e.

  • CreatedOn
  • CreatedBy
  • ModifiedOn
  • ModifiedBy

I currently have created a new sub-class in all my viewmodels with .AuditStamps that contains these properties, populate that in my controller and pass it to the partial.

Here is an example of one of my view Models. Note the AudiStamps sub-class

public class ItemViewModel
    {
        public Item Item { get; set; }
        public AuditStampsViewModel AuditStamps { get; set; }
    }

Here is my partial, suing this sub-class

@model OTIS.AppServ.Shared.AuditStampsViewModel

<hr />
<div class="twoColumn floatLeft">
    <div class="editor-field">
        <b>Created By:</b>
        @Html.DisplayFor(model => model.CreatedByName) on @Html.DisplayFor(model => model.CreatedOn)            
    </div>
</div>
<div class="twoColumn floatLeft">
    <div class="editor-field" style="text-align:right;">
        <b>Modified By:</b>
        @Html.DisplayFor(model => model.ModifiedByName) on @Html.DisplayFor(model => model.ModifiedOn)

    </div>
</div>

But I was wondering since all my classes will have these common properties, do I really need to go through adding and populating this sub-class. The only reason I am, is because I couldn't figure out how to declare a generic model in the partial...I was hoping the partial would just work if I didn't declare the model, but it doesn't. I get an error:

An expression tree may not contain a dynamic operation

How can I achieve this? Seems like something an Interface would solve just using classes, but not sure if the same concept applies using Partials (i.e. stating a partial implements an IAuditStamps inteface???)

4

2 に答える 2

2

最後に、パーシャルのモデルをインターフェイスに設定し、クラスが次のようにインターフェイスも実装していることを確認します。

IAuditStamps インターフェイスを作成する

public interface IAuditStamps
    {
        int CreatedById { get; set; }
        string CreatedByName { get; set; }
        DateTime CreatedOn { get; set; }
        int ModifiedById { get; set; }
        string ModifiedByName { get; set; }
        DateTime ModifiedOn { get; set; }
    }

インターフェースを実装する私のクラス

public partial class Item : IEntityId, ICompanyId, IAuditStamps

ItemViewModel (上記のクラス Item を使用)

public class ItemViewModel
    {
        public Item Item { get; set; }
    }

パーシャル

@model OTIS.domain.IAuditStamps

<hr />
<div class="twoColumn floatLeft">
    <div class="editor-field">
        <b>Created By:</b>
        @Html.DisplayFor(model => model.CreatedByName) on @Html.DisplayFor(model => model.CreatedOn)            
    </div>
</div>
<div class="twoColumn floatLeft">
    <div class="editor-field" style="text-align:right;">
        <b>Modified By:</b>
        @Html.DisplayFor(model => model.ModifiedByName) on @Html.DisplayFor(model => model.ModifiedOn)

    </div>
</div>

パーシャルの呼び出し:

@Html.Partial("_AuditStamps", Model.Item)
于 2012-12-26T18:34:27.760 に答える
1

ViewModelこれは、すべてのビュー モデルが継承できる基本クラスの最有力候補です。

public abstract class ViewModel 
{
    public DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime ModifiedOn { get; set; }
    public string ModifiedBy { get; set; }
}

public class ItemViewModel : ViewModel
{
    public Item Item { get; set; }
}

パーシャルはViewModel、関連するプロパティにバインドするモデル タイプとして を持ちます。パーシャルを必要とするすべてのビューは、ベース ViewModel からモデルを継承する必要があるため、プロパティはすべてモデル バインディングで使用できます。

于 2012-12-24T22:21:56.917 に答える