Asp.net-Mvc-3
使用介面作為局部視圖的模型類型+數據註釋
我有一個複雜的局部視圖需要不同的欄位驗證的情況,具體取決於使用局部視圖的位置。
我想我可以通過使局部視圖將介面作為模型類型並基於該介面實現兩個不同的 ViewModel 來解決這個問題。兩個 ViewModel 中的數據註釋會有所不同。然後,我會為局部視圖提供正確 ViewModel 的實例。
但我發現唯一能辨識的註釋是界面本身的註釋。介面實現 ViewModel 類上的 DA 被忽略,即使這些是作為模型傳遞的對象。所以我的計劃行不通。
有沒有解決的辦法?更好的方法?如果可以避免的話,我不希望將部分視圖拆分為單獨的視圖。
ETA:這是部分視圖的縮寫版本,根據要求:
@model IPerson @Html.ValidationSummary(false) <fieldset> <table class="editForm"> <tr> <td class="editor-label"> @Html.LabelFor(model => model.FirstName) </td> <td class="editor-field"> @Html.EditorFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName) </td> <td class="editor-label"> @Html.LabelFor(model => model.LastName) </td> <td class="editor-field"> @Html.EditorFor(model => model.LastName) @Html.ValidationMessageFor(model => model.LastName) </td> </tr> </table> <fieldset>真正的局部視圖很長,並且有很多 @if 語句來管理可選部分的呈現(或不呈現),但它並沒有做任何棘手的事情。
我的想法行不通: 這個執行緒提醒我,類不會從它們的介面繼承屬性。(正如答案所指出的,如果兩個介面指定了具有不同屬性的相同屬性,並且都由一個類實現,會發生什麼?)
它可能適用於通用基類。我明天試試。
謝謝大家。
安,你是對的。我已經刪除了我的評論。您不能通過您的視圖發回界面。但是,我不知道您到底想做什麼,因為我看不到您的程式碼。也許是這樣的?我將一個介面傳遞給視圖,但將它作為我期望的類傳遞回來。同樣,我不確定應用程序是否在這裡。
假設您有這樣的課程:
[MetadataType(typeof(PersonMetaData))] public class Customer : IPerson { public int ID { get; set; } public string Name { get; set; } [Display(Name = "Customer Name")] public string CustomerName { get; set; } } public class Agent : IPerson { public int ID { get; set; } public string Name { get; set; } } public partial class PersonMetaData : IPerson { [Required] public int ID { get; set; } [Required] [Display(Name="Full Name")] public string Name { get; set; } } public interface IPerson { int ID { get; set; } string Name { get; set; } } public interface IAgent { int AgentType { get; set; } } public interface ICustomer { int CustomerType { get; set; } }你的控制器看起來像:
public ActionResult InterfaceView() { IPerson person = new Customer { ID = 1 }; return View(person); } [HttpPost] public ActionResult InterfaceView(Customer person) { if (ModelState.IsValid) { TempData["message"] = string.Format("You posted back Customer Name {0} with an ID of {1} for the name: {2}", person.CustomerName, person.ID, person.Name); } return View(); }你的視圖看起來像這樣:
@model DataTablesExample.Controllers.Customer <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @if (@TempData["message"] != null) { <p>@TempData["message"]</p> } @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>IPerson</legend> @Html.HiddenFor(model => model.ID) <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div class="editor-label"> @Html.LabelFor(model => model.CustomerName) </div> <div class="editor-field"> @Html.EditorFor(model => model.CustomerName) @Html.ValidationMessageFor(model => model.CustomerName) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> }