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

  • Question

  • 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 .

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using BookingNile;
    using System.IO;
    
    namespace BookingNile.Controllers
    {
        public class MainMenusController : Controller
        {
            private BookingSystem db = new BookingSystem();
    
            // GET: MainMenus
            public ActionResult Index()
            {
                return View(db.MainMenus.ToList());
            }
    
    
            // GET: MainMenus/Create
            public ActionResult Create()
            {
                return View();
            }
    
            // POST: MainMenus/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "ID,MenuData,Title,Description,MetaTage,Slug,Subject,Parent,Text,IsActive,MenuID")] MainMenu mainMenu)
            {
                if (ModelState.IsValid)
                {
                    db.MainMenus.Add(mainMenu);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(mainMenu);
            }
    
         
    
          
            [HttpPost]
            public ActionResult Upload(HttpPostedFileBase file)
            {
                try
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        string filename = Path.GetFileName(file.FileName);
                        string path = Path.Combine(Server.MapPath("~/Images"), filename);
                        file.SaveAs(path);
                        ViewBag.Result = "File Uploaded Successfully";
                    }
                    else
                    {
                        ViewBag.Result = "No File Uploaded";
                    }
                }
                catch
                {
                    ViewBag.Result = "File upload failed";
                }
                return View();
            }
           
    
           
    
          
        }
    }

    Create View

    @model BookingNile.MainMenu
    
    @{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/DashBoard.cshtml";
    }
    
    <h2>Create</h2>
    <div class="form-horizontal2">
    
                @using (Html.BeginForm())
                {
                    @Html.AntiForgeryToken()
    
                    <div class="form-horizontal">
                        <h4>MainMenu</h4>
                        <hr />
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                       
    
    
    
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
                            </div>
                        </div>
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                            </div>
                        </div>
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.MetaTage, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.MetaTage, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.MetaTage, "", new { @class = "text-danger" })
                            </div>
                        </div>
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.Slug, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.Slug, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Slug, "", new { @class = "text-danger" })
                            </div>
                        </div>
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.Subject, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.Subject, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Subject, "", new { @class = "text-danger" })
                            </div>
                        </div>
    
                    
                        <div class="form-group">
                            @Html.LabelFor(model => model.Text, new { @class = "col-md-2 control-label" })
                            <div class="col-md-10">
                                @Html.TextBoxFor(model => model.MenuData, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.MenuData, "", new { @class = "text-danger" })
                            </div>
                        </div>
                     
    
                        <div class="form-group">
                            @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.IsActive, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div>
                            @using (Html.BeginForm("Upload", "MainMenus", FormMethod.Post, new { enctype = "multipart/form-data" }))
    
    {
                                <span>Select File:</span>
                                <input type="file" name="postedFile" />
                                <hr />
                                <input type="submit" value="Upload" />
                                <br />
                                <span style="color:green">@ViewBag.Message</span>
                            }
                        </div>
                      
    
    
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Create" class="btn btn-default" />
                            </div>
                        </div>
                    </div>
                }
    
                <div>
                    @Html.ActionLink("Back to List", "Index")
                </div>
            </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 



    Saturday, September 9, 2017 9:18 PM

All replies

  • Maybe you should post to the below forum.

    https://forums.asp.net/1146.aspx/1?MVC

    Saturday, September 9, 2017 9:38 PM
  • Hi,

    After reading your post, your question looks like not about WPF, as DA924x suggested, please post on MVC forum:

    https://forums.asp.net/1146.aspx/1/20?MVC , you will get a more professional support from there.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Bob Ding Monday, September 11, 2017 1:48 AM
    Monday, September 11, 2017 1:48 AM