mvc4上传文件我无法调用我的动作

时间:2016-01-05 21:14:43

标签: asp.net-mvc asp.net-mvc-4 image-uploading actionresult

我做了一个动作并使用Html.BeginForm上传图像文件
我的控制器名称是 ImagesTaple
就像那样:

    using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using bootstrab1.Models;

namespace bootstrab1.Controllers
{
    public class ImagesTableController : Controller
    {
        private SpaDbEntities db = new SpaDbEntities();

        //
        // GET: /Images/

        public ActionResult Index()
        {
            return View(db.C_Images.ToList());
        }

        [HttpPost]
        public ActionResult UploadImg(HttpPostedFileBase image)
        {
            if (image.ContentLength > 0)
            {
                var FileFame = Path.GetFileName(image.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/"), FileFame);
                image.SaveAs(path);
            }
            return RedirectToAction("Create");
        }
        //
        // GET: /Images/Details/

        public ActionResult Details(int id = 0)
        {
            C_Images c_images = db.C_Images.Find(id);
            if (c_images == null)
            {
                return HttpNotFound();
            }
            return View(c_images);
        }

        //
        // GET: /Images/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Images/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(C_Images c_images)
        {
            if (ModelState.IsValid)
            {
                db.C_Images.Add(c_images);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(c_images);
        }

        //
        // GET: /Images/Edit/5

        public ActionResult Edit(int id = 0)
        {
            C_Images c_images = db.C_Images.Find(id);
            if (c_images == null)
            {
                return HttpNotFound();
            }
            return View(c_images);
        }

        //
        // POST: /Images/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(C_Images c_images)
        {
            if (ModelState.IsValid)
            {
                db.Entry(c_images).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(c_images);
        }

        //
        // GET: /Images/Delete/5

        public ActionResult Delete(int id = 0)
        {
            C_Images c_images = db.C_Images.Find(id);
            if (c_images == null)
            {
                return HttpNotFound();
            }
            return View(c_images);
        }

        //
        // POST: /Images/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            C_Images c_images = db.C_Images.Find(id);
            db.C_Images.Remove(c_images);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

我的BeginForm就像那样

@using (Html.BeginForm("UploadImg","ImagesTaple", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <table>
<tr>
    <td>file : </td>
    <td><input type="file" name="File" id="file" /></td>
</tr>
<tr>
     <td>&nbsp;</td>
     <td><input type="submit" name="submet" value="upload" /></td>
</tr>
  </table>  

}

我的行动是

 [HttpPost]
        public ActionResult UploadImg(HttpPostedFileBase image)
        {
            if (image.ContentLength > 0)
            {
                var FileFame = Path.GetFileName(image.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/"), FileFame);
                image.SaveAs(path);
            }
            return RedirectToAction("Create");
        }

现在当我按下子网按钮时 我得到这个错误消息

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /ImagesTaple/UploadImg

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

我的代码plz有什么问题 thanx求助你

1 个答案:

答案 0 :(得分:0)

我只是拼写错误,将ImagesTaple更改为ImagesTable

相关问题