Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. View not found when a view model is specified
  2. using MyProject.ViewModels;
  3.  
  4. [ChildActionOnly]
  5. public PartialViewResult ShowMyView(int id)
  6. {
  7.   return PartialView(new MyModel() { ModelID = id });
  8. }
  9.        
  10. namespace MyProject.ViewModels
  11. {
  12.   [Bind(Exclude = "ModelID")]
  13.   public class MyModel: IValidatableObject
  14.   {
  15.     public MyModel()
  16.     {
  17.         Count = 1;
  18.     }
  19.  
  20.     [Required]
  21.     [HiddenInput(DisplayValue = false)]
  22.     public int ModelID { get; set; }
  23.  
  24.     [Required]
  25.     [Range(1, 9999)]
  26.     public int Count { get; set; }
  27.  
  28.     public IEnumerable<ValidationResult> Validate(
  29.                   ValidationContext validationContext)
  30.     {
  31.         if (ModelID <= 0)
  32.             yield return new ValidationResult("Model ID missing",
  33.                                           new[] { "ModelID" });
  34.  
  35.         if (Count <= 0)
  36.             yield return new ValidationResult("Count cannot be zero",
  37.                                           new[] { "Count" });
  38.     }
  39.   }
  40. }
  41.        
  42. @inherits WebViewPage
  43.        
  44. @inherits WebViewPage<MyProject.ViewModels.MyModel>
  45.        
  46. @inherits WebViewPage<MyProject.ViewModels.MyModel>
  47.        
  48. @model MyProject.ViewModels.MyModel
  49.        
  50. @Html.Action("ShowMyView", "SomeController", new { id = "123" })