asp.net mvc 4 - Return to the same view when no image is selected -


i have upload control. if user didnt select image , press on upload button, user message he/she has go , try again, this:

if (issavedsuccessfully)             {                 return redirect(url.action("edit", "account") + "#tabs-2");             }             else             {                 return json(new { message = "error in saving file, go ,  try again" });             } 

but ofcourse not want. because message in seperate view, not in view self, can upload image. how show message in same view, upload image?

this full method:

[httppost]         public actionresult editphotos(userprofile user, httppostedfilebase file)         {                         bool issavedsuccessfully = true;             try             {                  if (file != null || file.contentlength > 0)                 {                     if (isimage(file) == false)                     {                         // invalid file type                         return json(new { message = "error in saving file, go ,  try again" });                     }                     int ifilesize = file.contentlength;                     if (ifilesize > 3048576)  // 1mb                     {                         // file exceeds file maximum size                         return json(new { message = "image large , go ,  try again" });                     }                                    }                  if (modelstate.isvalid)                 {                     var job = new imagejob(file, "~/images/profile/<guid>", new instructions("mode=max;format=jpg;width=400;height=400;"));                     job.createparentdirectory = true;                     job.addfileextension = true;                     job.build();                      var dirseparator = path.directoryseparatorchar;                     var filename = path.getfilename(job.finalpath);                      if (modelstate.isvalid)                     {                         string username = user.identity.name;                                                 user = db.userprofiles.firstordefault(u => u.username.equals(username));                         user.image = new byte[file.contentlength];                         file.inputstream.read(user.image, 0, file.contentlength);                     }                     // update fields                                     user.imagemimetype = file.contenttype;                     db.entry(user).state = entitystate.modified;                     db.savechanges();                 }             }             catch (exception ex)             {                 issavedsuccessfully = false;              }              if (issavedsuccessfully)             {                 return redirect(url.action("edit", "account") + "#tabs-2");             }             else             {                 return json(new { message = "error in saving file, go ,  try again" });             }          } 

and view upload image:

@model contosouniversity.models.userprofile @using contosouniversity.source @{     viewbag.title = "edit"; } <div id="tabs-2">     @using (html.beginform("editphotos", "account", formmethod.post, new { id = "form_id", enctype = "multipart/form-data" }))     {         @html.antiforgerytoken()          <div class="form-horizontal">             <h4>profile photo</h4>             <hr />              @html.hiddenfor(model => model.id)              <div id="upload-choices">                 <div class="editor-label">                     @html.validationmessagefor(model => model.image)                 </div>                 <div class="editor-row">                     @html.validationsummary(true)                 </div>             </div>             <br />             upload profile image not bigger 3 mb             <table class="table">                 <tr>                     @if (model.image == null)                     {                         <th><img class="pull-left" src="~/images/rockonbycicle.png" width="200" height="150" alt="rockcycle" /></th>                      }                     else                     {                         <th><img width="200" height="150" src="@url.action("getimage", "account", new { id =  model.id })"></th>                      }                 </tr>             </table>             <input type="file" name="file" class="filestyle" data-icon="false">             <br />             <div class="progress progress-striped">                 <div class="progress-bar progress-bar-success">0%</div>             </div>              <div id="status"></div>             <br />                       <div class="pull-left">                 <div class="col-md-offset-0">                     <input type="submit" value="upload" accept="image/x-png, image/gif, image/jpeg" class="btn btn-default pull-left" />                  </div>             </div>          </div>     }      <br /><br /><br />     <div>         @html.actionlink("back list", "index")     </div>  </div>  @section scripts {     <link href="@url.content("~/content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />     <link href="@url.content("~/content/site.css")" rel="stylesheet" type="text/css" />     <script src="@url.content("~/scripts/jquery-1.11.0.min.js")" type="text/javascript"></script>     <script src="@url.content("~/scripts/jquery.form.js")" type="text/javascript"></script>     <script src="@url.content("~/scripts/jquery-ui-1.10.4.min.js")" type="text/javascript"></script>     <script src="@url.content("~/scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>     <script src="@url.content("~/scripts/bootstrap-filestyle.js")" type="text/javascript"></script>     <script src="@url.content("~/scripts/jquery.form.min.js")" type="text/javascript"></script>      <script type="text/javascript">     </script> } 

thank you

add modelstate error , return view (otherwise redirect), example

if (issavedsuccessfully) {     return redirect(url.action("edit", "account") + "#tabs-2"); } else {     modelstate.addmodelerror("image", "your message");     return view(user); } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -