asp.net mvc - Validating a pure HTML form in mvc -


is there way validate pure html form in view no model in mvc?

my view looks

@using (html.beginform("upload", "picture", formmethod.post, new { enctype = "multipart/form-data" }))     {         @html.antiforgerytoken()         <a class="uploadlink" style="cursor:pointer" onclick="document.getelementbyid('file').click();">open</a>         <input type="file" name="file" id="file" style="opacity:0" onchange="document.getelementbyid('title').value = this.value.substring(this.value.lastindexof('\\') +1 );"/>         <br />         <input type="text" name="title" id="title" />         <br/>         <textarea name="desc" id="desc"></textarea>         <br/>         <input type="submit" value="save" />     } 

my controller looks like

[httppost]         [validateantiforgerytoken]         public actionresult upload(string title, string desc, httppostedfilebase file)         {              if (file == null)             {                 return content("<span id='result'>please select file first</span>");             }             if (string.isnullorempty(title))             {                 return content("<span id='result'>please enter name</span>");             }             if (file.contentlength > 0)             {                 var filename = system.io.path.getfilename(file.filename);                 string c = file.filename.substring(file.filename.lastindexof("."));                 title = title.replace(c, "");                 byte[] uploadedfile = new byte[file.inputstream.length];                 file.inputstream.read(uploadedfile, 0, uploadedfile.length);                 try                 {                     using (memorystream ms = new memorystream(uploadedfile))                     image.fromstream(ms);                 }                 catch (argumentexception)                 {                     return content("<span id='result'>the file trying upload not valid image file.</span>");                 } 

instead of return content wondering if there way add modelstate.adderror or similar.

yeah, add modelstate.addmodelerror in view should use of html helpers such html.validationsummary or html.validationmessage if want message appear.

for example in view:

<input type="file" name="file" id="file" style="opacity:0" onchange="document.getelementbyid('title').value = this.value.substring(this.value.lastindexof('\\') +1 );"/> @html.validationmessage("file") 

and in controller action:

modelstate.addmodelerror("file", "some error message"); 

obviously it's better use view models , typed equivalents of helpers using helpers generate markup instead of hardcoding did in case.

you know, things like:

public class myviewmodel {     [required(errormessage = "please select file first")]     public httppostedfilebase file { get; set; }      [required(errormessage = "please enter name")]     public string title { get; set; }      public string description { get; set; } } 

and in view things like:

@model myviewmodel ...  @using (html.beginform("upload", "picture", formmethod.post, new { enctype = "multipart/form-data" })) {     @html.antiforgerytoken()      <a class="uploadlink" style="cursor:pointer" onclick="document.getelementbyid('file').click();">open</a>      @html.textboxfor(x => x.file, new {          type = "file",          style = "opacity:0",          onchange="document.getelementbyid('title').value = this.value.substring(this.value.lastindexof('\\') +1 );"      })     @html.validationmessagefor(x => x.file)     <br />      @html.editorfor(x => x.title)     @html.validationmessagefor(x => x.title)     <br/>      @html.textareafor(x => x.description)     <br/>      <input type="submit" value="save" /> } 

and in controller action:

[httppost] [validateantiforgerytoken] public actionresult upload(myviewmodel model) {     if (!modelstate.isvalid)          {         return view(model);     }      try     {         image.fromstream(model.file.inputstream);     }     catch (argumentexception)     {         modelstate.addmodelerror("file", "the file trying upload not valid image file.");     }      // @ stage know model valid =>     // model.title , model.description properties      return redirecttoaction("success"); } 

by way might take @ following answer of mine rid of more plumbing/validation code controller action doesn't belong there.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -