Tuesday 15 June 2010

asp.net mvc - Custom HttpAttribute Validation when override IsValid(object value) is always null -


Hello, this is my custom verification but confirm with a little different on the type of property, and always if I trade rules The value of ModelState is "zero" = s ... here is some code

model

  [System.ComponentModel.DataAnnotations.Schema.Column (TypeName = "Image" )] [Valid data file (allowed = new string [] {".png", ".jpeg", ".jpg"}, maxlend = 1024 * 1024 * 3, error message = "please select a PNG, JPEG, JPG image Less than 3 MB ")] Public Byte [] Photos {get; Set; }   

===========

Valid file edit

  Public class valid FIFF feature: Required item {Public Integrate Lamp {Received; Set; } Public string [] allowed {get; Set; } Public Override Bull ISAid (Object ** Price **) {var photo = value as HttpPostedFileBase; If (photo == faucet) {return false; } If (Photo Content Lang & Maxthong) {return false; } If (! Allowed! Suggestions (Photo, Filimon. Substring (Photo. Filln .list Indexoff ('.'))) {Return false; } Back true; }   

Where should not be a tap anywhere!

Create.cshtml

  @using (Html.BeginForm ("Create", "Home", FormMethod.Post, New {enctype = "multipart / form-data" }) {@ HTML.ValidationSummary (true) & lt; Fieldet & gt; & Lt; Story & gt; Restaurant & lt; / Narrative & gt; & Lt; Div class = "editor-label" & gt; @ Html.LabelFor (model => model.Name) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model = & gt; model.Name) @ html.ValidationMessageFor (model = & gt; model.Name) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @HTML LabelFor (model = & gt; model.ct) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model => model.City) @ Html.ValidationMessageFor (model = & gt; model.City) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @ Html.LabelFor (model = & gt; model.Country) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model => model.Country) @ Html.ValidationMessageFor (model => model.Country) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @htmllabelsfor (model => model. Photos) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @html EditorFor (model => model.photo) @html. Validity message (model => model. Photo) & lt; Input name = "ImageFile" type = "file" id = "ImageFile" /> & Lt; / Div & gt; & Lt; P & gt; & Lt; Input type = "submit" value = "create" /> & Lt; / P & gt; & Lt; / Fieldset & gt; }   

Any help?

your property:

  public byte [] photos {get; Set; }   

in the attribute:

  var photo = value in the form of a HTTP post filename;   

Try it will always be empty:

  var photo = value as bite []   

Definitely You have to fix the addtional parts of your verification logic.

No comments:

Post a Comment