![](/img/trans.png)
[英]UNITY C# - How can I get all the fields of the an instance of a class marked with my custom atribute(the fields)?
[英]Custom ViewModel Class - Not all fields are marked invalid unless a prefix is specified
我有一個自定義viewmodel,其中有兩個字段和一個linq2sql實體..所有字段都附加有驗證屬性。 即使所有字段均無效,也僅在視覺上指示linq2sql類中的字段是否存在錯誤,並且視圖模型中的字段將正常顯示。 但是錯誤消息顯示為所有無效的字段。
我的自定義ViewModel如下所示:
public class BooksViewModel
{
public SelectList BookCategories { get; set; }
public Book Book { get; set; }
[Required(ErrorMessage="Field1 is required")]
[StringLength(10)]
public string Field1 { get; set; }
[Required(ErrorMessage = "Field2 question is required")]
[StringLength(100)]
public string Field2 { get; set; }
}
Book類是linq2sql實體,該實體具有用於驗證的元數據類型屬性。
[MetadataType(typeof(BookMetadata))]
public partial class Book
{
}
public class BookMetadata
{
[Required(ErrorMessage="Choose a category")]
public int CategoryID { get; set; }
[Required(ErrorMessage = "Title is required")]
[StringLength(100)]
public string Title { get; set; }
[Required(ErrorMessage = "Published date is required")]
[DataType(DataType.Date, ErrorMessage="Enter a valid date")]
public DateTime PublishedDate { get; set; }
[Required(ErrorMessage = "Author is required")]
[StringLength(50)]
public string Author { get; set; }
}
存儲庫中有一個AddBook方法,有兩個重載。 一個使用viewmodel,一個使用Book類型:
public void AddBook(Book book)
{
var errors = DataAnnotationsValidationRunner.GetErrors(book);
if (errors.Any()) {
throw new RulesException(errors);
}
_db.Books.InsertOnSubmit(book);
_db.SubmitChanges();
}
public void AddBook(BooksViewModel model)
{
var errors = DataAnnotationsValidationRunner.GetErrors(model);
if (errors.Any()) {
throw new RulesException(errors);
}
}
控制器中的Create動作如下所示:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Book.ID")]BooksViewModel booksViewModel)
{
try {
// Validate Book
_repository.AddBook(booksViewModel.Book);
} catch(RulesException ex) {
ex.AddModelStateErrors(ModelState, "Book");
}
try {
// Validate other fields in the view model
_repository.AddBook(booksViewModel);
} catch (RulesException ex) {
ex.AddModelStateErrors(ModelState, "");
}
if (ModelState.IsValid) {
return RedirectToAction("Index");
} else {
booksViewModel.BookCategories = new SelectList(_repository.GetAllCategories().AsEnumerable(), "ID", "CategoryName", booksViewModel.Book.CategoryID);
return (ActionResult)View(booksViewModel);
}
}
我正在使用xVal生成客戶端驗證規則..我的創建視圖如下所示:
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Book.CategoryID">CategoryID:</label>
<%= Html.DropDownList("Book.CategoryID", Model.BookCategories, "Select")%>
<%= Html.ValidationMessage("Book.CategoryID", "*")%>
</p>
<p>
<label for="Book.Title">Title:</label>
<%= Html.TextBox("Book.Title")%>
<%= Html.ValidationMessage("Book.Title", "*")%>
</p>
<p>
<label for="Book.PublishedDate">PublishedDate:</label>
<%= Html.TextBox("Book.PublishedDate")%>
<%= Html.ValidationMessage("Book.PublishedDate", "*")%>
</p>
<p>
<label for="Book.Author">Author:</label>
<%= Html.TextBox("Book.Author")%>
<%= Html.ValidationMessage("Book.Author", "*")%>
</p>
<p>
<label for="Field1">Field1:</label>
<%= Html.TextBox("Field1")%>
<%= Html.ValidationMessage("Field1", "*")%>
</p>
<p>
<label for="Field2">Field2:</label>
<%= Html.TextBox("Field2")%>
<%= Html.ValidationMessage("Field2", "*")%>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
<%= Html.ClientSideValidation<BooksViewModel>() %>
<%= Html.ClientSideValidation<Book>("Book") %>
客戶端驗證工作正常..但是,如果我關閉javascript,然后回發表單錯誤消息,則會顯示所有字段,但Field1和Field2不會標記為無效..沒有將CSS類添加到字段,並且沒有生成跨度標簽以用於視覺錯誤指示。
截圖http://img22.imageshack.us/img22/324/26677634.jpg
但是如果我用任何東西固定ViewModel字段
public ActionResult Create([Bind(Prefix = "VM", Exclude = "Book.ID")]BooksViewModel booksViewModel)
並相應地修改視圖,然后一切正常:
<p>
<label for="VM.Book.Title">Title:</label>
<%= Html.TextBox("VM.Book.Title")%>
<%= Html.ValidationMessage("VM.Book.Title", "*")%>
</p>
<p>
<label for="VM.Field1">Field1:</label>
<%= Html.TextBox("VM.Field1")%>
<%= Html.ValidationMessage("VM.Field1", "*")%>
</p>
<%= Html.ClientSideValidation<BooksViewModel>("VM") %>
<%= Html.ClientSideValidation<Book>("Book") %>
我在這里做錯了什么?
很抱歉使這篇文章這么長..
我認為問題可能出在您的變量名和類名相同。
也許在您的ViewModel中重命名為類似的內容,
public Book BookInstance { get; set; }
這將需要一些重構,但是我認為這是造成您問題的原因。
善良,
擔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.