2012-08-16 52 views
1

我有一個圖像上傳編輯器模板,它目前強類型爲MultiImageUploader視圖模型。問題是我有一些自定義的數據驗證屬性,我想在直接調用編輯器模板的視圖模型中使用,而不是通過MultiImageUploader視圖模型進行路由。我可以在EditorTemplate中動態分配@model類型嗎?

而不是調用預設驗證屬性MultiImageUploader視圖模型的,我想要做這樣的事情:

public class CreateBrandViewModel 
{ 
    .....<snipped>..... 

    [PermittedFileExtensions("jpg, jpeg, png, gif")] 
    [MaxFileSize("2MB")] 
    [UIHint("MultiImageUploader")] 
    public HttpPostedFileBase Image { get; set; } 

    //Currently this view model looks like this: 
    //public MultiImageUploader Image { get; set; } <-- seperate view model 

} 

我不能使用,目前我的首選方式,因爲我的編輯模板是不是強輸入CreateBrandViewModel。有沒有一種方法可以將調用視圖模型的@model傳遞到編輯器模板視圖中? :

@model // Here? // 
<div class="editor-field"> 
    @Html.TextBoxFor(x => x.Image, new { type = "file" }) 
    @Html.ValidationMessageFor(x => x.Image) 
</div> 

編輯1

只是爲了澄清,我想這樣做的原因是因爲我想從一個圖片上傳更改爲通用的文件上傳 - 這就需要不同驗證屬性在不同的視圖模型上。要做到這一點目前我需要爲每個略有不同的驗證參數變化創建一個不同的視圖模型和編輯器模板。

編輯2:關於@Joel Athertons回答

我碰到的努力來實現這個(或者也許我只是沒有正確理解)的幾個問題。

我已經創建了接口和抽象類。我的CreateBrandViewModel現在繼承自FileUpload。 FileUpload目前是空的,沒有共享屬性。當我嘗試model.GetType()。名稱我得到一個「對象引用未設置爲對象的實例。」錯誤。代碼如下:

控制器傳遞一個CreateBrandViewModel的觀點:

[HttpGet] 
    public ActionResult Create() 
    { 
     var model = new CreateBrandViewModel(); 
     model.IsActive = true; 

     return View(model); 
    } 

創建視圖然後調用EditorTemplate:

@model CumbriaMD.Infrastructure.ViewModels.BrandViewModels.CreateBrandViewModel 

@Html.EditorFor(model => model.File, "EditorTemplates/MultiImageUploader") 

模板然後就(對純樸的目的)的樣子這:

@model CumbriaMD.Infrastructure.ViewModels.FileUploadViewModels.FileUpload 

@{ 
    var partialView = Model.GetType().Name; 
} 

<h1>@partialView</h1> 

任何想法,將不勝感激:)

回答

1

我將處理這種方式將有一個接口,所有的模型,w不允許上傳會執行。如果你實際上有共同的功能,我也會將它與具有任何共同屬性,屬性等的抽象模型耦合起來。然後,您的每個模型都將繼承抽象類(或者如果您沒有使用類,則實現該接口),然後可以將其用作@model語句。然後,您可以簡單地將任何一次性部分拆分爲可以做他們自己的事情的部分視圖。

public interface IFileUploadModel 
{ 
    // any common properties would go here 
} 

public abstract class FileUploadModel : IFileUploadModel 
{ 
    // implement the common stuff 
} 

public class CreateBrandViewModel : FileUploadModel 
{ 
    [PermittedFileExtensions("jpg, jpeg, png, gif")] 
    [MaxFileSize("2MB")] 
    [UIHint("MultiImageUploader")] 
    public HttpPostedFileBase Image { get; set; } 
} 

public class SomeOtherUploadModel : FileUploadModel 
{ 
    // Other special stuff here 
} 

然後在您的模板中。

@model FileUploadModel 

@{ 
    // Common output code that they all do 

    // Then the special stuff 
    if (model.GetType().Name == "CreateBrandViewModel") 
    { 
     // Render the partial and pass it the model 
     Html.RenderPartial("CreateBrandPartialView", Model); 
    } 
} 
+0

嘿,對不起,需要一段時間來回復 - 昨天被捆綁了!這看起來非常感謝,好主意。將於今天上午實施。有一件事,if語句 - 我將不得不爲每個實現FileUploadModel的模型編寫if語句(考慮到我還沒有嘗試過)?謝謝! – Kiada 2012-08-17 06:34:41

+0

hrrmm我想我現在明白了一點。但是,如果我能得到模型的類型並使用嚴格的命名約定,我甚至可以將model.GetType()正則表達式的名稱字符串移除「ViewModel」並將其放入Html.RenderPartial(regExResult +「PartialView ),模型);? – Kiada 2012-08-17 09:00:42

+0

遇到幾個問題實現這個好主意,更新原始文章:) – Kiada 2012-08-17 09:55:12

0

如果你的類看起來是這樣的:

public class CreateBrandViewModel 
{ 
    [PermittedFileExtensions("jpg, jpeg, png, gif")] 
    [MaxFileSize("2MB")] 
    [UIHint("MultiImageUploader")] 
    public HttpPostedFileBase Image { get; set; } 
} 

,你有一個名爲MultiImageUploader.cshtml的EditorTemplate,那麼這將是這樣的:

@model HttpPostedFileBase 

@Html.LabelFor(m => m) 
@Html.TextBoxFor(x => x, new { type = "file" }) 
@Html.ValidationMessageFor(x => x) 

你會呈現在你的整體視圖與:

@Html.EditorFor(m => m.Image) 
+0

在我看來,這將產生一個編譯錯誤,因爲該類沒有繼承'HttpPostedFileBase',所以不能在'@ model'語句後面使用。 – 2012-08-16 10:33:56

+0

如果使用@ Html.EditorFor(m => m.Image),則不是,它是模型的Image屬性的編輯器模板。 – 2012-08-16 11:00:15

相關問題