Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The current request for action 'Upload' on controller type 'ImageGalleryController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Upload() on type MvcImageUpload.Controllers.ImageGalleryController
System.Web.Mvc.ActionResult Upload(MvcImageUpload.ImageGallery) on type MvcImageUpload.Controllers.ImageGalleryController

Exception Details: System.Reflection.AmbiguousMatchException: The current request for action 'Upload' on controller type 'ImageGalleryController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Upload() on type MvcImageUpload.Controllers.ImageGalleryController
System.Web.Mvc.ActionResult Upload(MvcImageUpload.ImageGallery) on type MvcImageUpload.Controllers.ImageGalleryController

Stack Trace: 

[AmbiguousMatchException: The current request for action 'Upload' on controller type 'ImageGalleryController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Upload() on type MvcImageUpload.Controllers.ImageGalleryController
System.Web.Mvc.ActionResult Upload(MvcImageUpload.ImageGallery) on type MvcImageUpload.Controllers.ImageGalleryController]
   System.Web.Mvc.Async.AsyncActionMethodSelector.FindAction(ControllerContext controllerContext, String actionName) +276
   System.Web.Mvc.Async.ReflectedAsyncControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) +181
   System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) +52
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +295
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +83
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +501
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +417
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +49
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +127
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +373
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcImageUpload.Controllers
{
    public class ImageGalleryController : Controller
    {
        //
        // GET: /ImageGallery/

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

      
 
            public ActionResult Gallery()
            {
                List<ImageGallery> all = new List<ImageGallery>();

                // Here MyDatabaseEntities is our datacontext
                using ( ImgUpEntities dc = new ImgUpEntities ())
                {
                    all = dc.ImageGalleries.ToList();
                }
                return View(all);
            }
            public ActionResult Upload()
            {
                return View();
            }

            public ActionResult Upload(ImageGallery IG)
            {
                // Apply Validation Here


                if (IG.File.ContentLength > (2 * 1024 * 1024))
                {
                    ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
                    return View();
                }
                if (!(IG.File.ContentType == "image/jpeg" || IG.File.ContentType == "image/gif"))
                {
                    ModelState.AddModelError("CustomError", "File type allowed : jpeg and gif");
                    return View();
                }

                IG.FileName = IG.File.FileName;
                IG.ImageSize = IG.File.ContentLength;

                byte[] data = new byte[IG.File.ContentLength];
                IG.File.InputStream.Read(data, 0, IG.File.ContentLength);

                IG.ImageData = data;
                using (ImgUpEntities dc = new ImgUpEntities())
                {
                    dc.ImageGalleries.Add(IG);
                    dc.SaveChanges();
                }
                return RedirectToAction("Gallery");
            }
        }
        
    }
Posted
Updated 21-Dec-15 21:45pm
v2

1 solution

Assuming that you're using the standard MVC constructs and principals, this can be easily fixed with attributes. Assuming that you're using GET for your view retrieval and POST for your form, which are the defaults, change your code like so:

C#
[HttpGet]
public ActionResult Upload()
{
   return View();
}
 
[HttpPost]
public ActionResult Upload(ImageGallery IG)
{
   ...
}


You're seeing this behavior because ImageGallery is a class, and therefore nullable. The attributes should clear it right up.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900