2013-05-10 87 views
3

我創建了聯繫人模塊,用戶在單擊提交按鈕時發送電子郵件。果園模塊 - 如何從驅動程序動態返回強類型模型

一切正常。但是,我很好奇,如果有可能返回一個強類型模型而不是動態類。

例如,以下是我的DRIVERS \ ContactUsDriver.cs顯示功能:

protected override DriverResult Display(ContactUsPart part, string displayType, dynamic shapeHelper) 
{ 
     return ContentShape("Parts_ContactUs", 
      () => shapeHelper.Parts_ContactUs(
       Name: part.Name)); 
} 

正如你所看到的,上面返回一個動態Parts_ContactUs。

現在,這裏是我的看法\配件\ ContactUs.cshtml的快照:

@model dynamic 

@using (Html.BeginForm("Send", "ContactUs", new { area = "ContactUs" }, FormMethod.Post)) 
{ 
    <fieldset> 
     <legend>Contact Us</legend> 
     <div id="contact-us" class="area"> 
      @Html.TextBox("Name", "") 
     </div> 
     <div id="submitArea" class="button">  
      <input type="submit" value="Submit Message"> 
     </div> 
    </fieldset> 
} 

正如你所看到的視圖上方勢必@model動態。其結果是,我必須做以下

@Html.TextBox("Name", "") 

有沒有我可以綁定到模型的方式說ContactUsModel,因此並以下呢?

@Html.TextBoxFor(m => m.Name) 

特別是,我很感興趣,所以我可以寫一個jQuery的DataAnnotation屬性驗證。

回答

3

完全有可能。只需提供所需的模型類型的第一個參數創建形狀時:

protected override DriverResult Display(
    ContactUsPart part, 
    string displayType, 
    dynamic shapeHelper) 
{ 
    return ContentShape("Parts_ContactUs", 
     () => shapeHelper.Parts_ContactUs(typeof(MyClass), Name: part.Name)); 
} 
+0

邊注:請注意,這需要'MyClass'從'Orchard.DisplayManagement.Shapes.Shape'或至少繼承當Name是'MyClass'的實際屬性時,它需要實現'IShape' **和**,由於以下問題,它將在視圖中爲空(Orchard 1.10.1中仍然存在問題,也有同樣的困難今天再次):https://github.com/OrchardCMS/Orchard/issues/5514 – ViRuSTriNiTy 2016-06-16 09:30:27