ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 277.8k

cannot make upload image to server and message success not d

Sep 9 2017 5:09 PM


cannot make upload image to server and message success not display .

I need to make upload image before add data in the following code i do three thing

1- upload image to server Images folder

2-show success image to upload image when click to upload button .(2 is problem )

3-insert data after upload image when click to create button

my problem is when click upload button it make redirect to index view and image not uploaded .
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Linq;  
  6. using System.Net;  
  7. using System.Web;  
  8. using System.Web.Mvc;  
  9. using BookingNile;  
  10. using System.IO;  
  11.   
  12. namespace BookingNile.Controllers  
  13. {  
  14.     public class MainMenusController : Controller  
  15.     {  
  16.         private BookingSystem db = new BookingSystem();  
  17.   
  18.         // GET: MainMenus  
  19.         public ActionResult Index()  
  20.         {  
  21.             return View(db.MainMenus.ToList());  
  22.         }  
  23.   
  24.   
  25.         // GET: MainMenus/Create  
  26.         public ActionResult Create()  
  27.         {  
  28.             return View();  
  29.         }  
  30.   
  31.         // POST: MainMenus/Create  
  32.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for   
  33.         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.  
  34.         [HttpPost]  
  35.         [ValidateAntiForgeryToken]  
  36.         public ActionResult Create([Bind(Include = "ID,MenuData,Title,Description,MetaTage,Slug,Subject,Parent,Text,IsActive,MenuID")] MainMenu mainMenu)  
  37.         {  
  38.             if (ModelState.IsValid)  
  39.             {  
  40.                 db.MainMenus.Add(mainMenu);  
  41.                 db.SaveChanges();  
  42.                 return RedirectToAction("Index");  
  43.             }  
  44.   
  45.             return View(mainMenu);  
  46.         }  
  47.   
  48.        
  49.   
  50.         
  51.         [HttpPost]  
  52.         public ActionResult Upload(HttpPostedFileBase file)  
  53.         {  
  54.             try  
  55.             {  
  56.                 if (file != null && file.ContentLength > 0)  
  57.                 {  
  58.                     string filename = Path.GetFileName(file.FileName);  
  59.                     string path = Path.Combine(Server.MapPath("~/Images"), filename);  
  60.                     file.SaveAs(path);  
  61.                     ViewBag.Result = "File Uploaded Successfully";  
  62.                 }  
  63.                 else  
  64.                 {  
  65.                     ViewBag.Result = "No File Uploaded";  
  66.                 }  
  67.             }  
  68.             catch  
  69.             {  
  70.                 ViewBag.Result = "File upload failed";  
  71.             }  
  72.             return View();  
  73.         }  
  74.          
  75.   
  76.          
  77.   
  78.         
  79.     }  
  80. }  
 Create View
  1. @model BookingNile.MainMenu  
  2.   
  3. @{  
  4.     ViewBag.Title = "Create";  
  5.     Layout = "~/Views/Shared/DashBoard.cshtml";  
  6. }  
  7.   
  8. <h2>Create</h2>  
  9. <div class="form-horizontal2">  
  10.   
  11.             @using (Html.BeginForm())  
  12.             {  
  13.                 @Html.AntiForgeryToken()  
  14.   
  15.                 <div class="form-horizontal">  
  16.                     <h4>MainMenu</h4>  
  17.                     <hr />  
  18.                     @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  19.                      
  20.   
  21.   
  22.   
  23.   
  24.                     <div class="form-group">  
  25.                         @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })  
  26.                         <div class="col-md-10">  
  27.                             @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })  
  28.                             @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })  
  29.                         </div>  
  30.                     </div>  
  31.   
  32.                     <div class="form-group">  
  33.                         @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })  
  34.                         <div class="col-md-10">  
  35.                             @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })  
  36.                             @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })  
  37.                         </div>  
  38.                     </div>  
  39.   
  40.                     <div class="form-group">  
  41.                         @Html.LabelFor(model => model.MetaTage, htmlAttributes: new { @class = "control-label col-md-2" })  
  42.                         <div class="col-md-10">  
  43.                             @Html.EditorFor(model => model.MetaTage, new { htmlAttributes = new { @class = "form-control" } })  
  44.                             @Html.ValidationMessageFor(model => model.MetaTage, "", new { @class = "text-danger" })  
  45.                         </div>  
  46.                     </div>  
  47.   
  48.                     <div class="form-group">  
  49.                         @Html.LabelFor(model => model.Slug, htmlAttributes: new { @class = "control-label col-md-2" })  
  50.                         <div class="col-md-10">  
  51.                             @Html.EditorFor(model => model.Slug, new { htmlAttributes = new { @class = "form-control" } })  
  52.                             @Html.ValidationMessageFor(model => model.Slug, "", new { @class = "text-danger" })  
  53.                         </div>  
  54.                     </div>  
  55.   
  56.                     <div class="form-group">  
  57.                         @Html.LabelFor(model => model.Subject, htmlAttributes: new { @class = "control-label col-md-2" })  
  58.                         <div class="col-md-10">  
  59.                             @Html.EditorFor(model => model.Subject, new { htmlAttributes = new { @class = "form-control" } })  
  60.                             @Html.ValidationMessageFor(model => model.Subject, "", new { @class = "text-danger" })  
  61.                         </div>  
  62.                     </div>  
  63.   
  64.                   
  65.                     <div class="form-group">  
  66.                         @Html.LabelFor(model => model.Text, new { @class = "col-md-2 control-label" })  
  67.                         <div class="col-md-10">  
  68.                             @Html.TextBoxFor(model => model.MenuData, new { @class = "form-control" })  
  69.                             @Html.ValidationMessageFor(model => model.MenuData, "", new { @class = "text-danger" })  
  70.                         </div>  
  71.                     </div>  
  72.                    
  73.   
  74.                     <div class="form-group">  
  75.                         @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })  
  76.                         <div class="col-md-10">  
  77.                             @Html.EditorFor(model => model.IsActive, new { htmlAttributes = new { @class = "form-control" } })  
  78.                             @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })  
  79.                         </div>  
  80.                     </div>  
  81.                     <div>  
  82.                         @using (Html.BeginForm("Upload", "MainMenus", FormMethod.Post, new { enctype = "multipart/form-data" }))  
  83.   
  84. {  
  85.                             <span>Select File:</span>  
  86.                             <input type="file" name="postedFile" />  
  87.                             <hr />  
  88.                             <input type="submit" value="Upload" />  
  89.                             <br />  
  90.                             <span style="color:green">@ViewBag.Message</span>  
  91.                         }  
  92.                     </div>  
  93.                     
  94.   
  95.   
  96.                     <div class="form-group">  
  97.                         <div class="col-md-offset-2 col-md-10">  
  98.                             <input type="submit" value="Create" class="btn btn-default" />  
  99.                         </div>  
  100.                     </div>  
  101.                 </div>  
  102.             }  
  103.   
  104.             <div>  
  105.                 @Html.ActionLink("Back to List", "Index")  
  106.             </div>  
  107.         </div>  
 

Main problem cannot upload image in code above

Actually i need when click upload button to image show success message and upload image to Images Folder .

but this not happen

what happen when click upload button redirect to index and when see folder Images not uploaded image selected exist


Answers (1)