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();
-
-
- public ActionResult Index()
- {
- return View(db.MainMenus.ToList());
- }
-
-
-
- public ActionResult Create()
- {
- return View();
- }
-
-
-
-
- [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