Showing posts with label AP.NET MVC3. Show all posts
Showing posts with label AP.NET MVC3. Show all posts

Sunday, October 27, 2013

ASP.NET MVC Structure Map, No parameter less constructor defined for this object.

While using Structure Map with ASP.NET MVC there was a error "No parameter less constructor defined for this object" for the already working controller. 

 [HttpPost]
        public ActionResult CreateBranch(mBranch branch)
        {
            _service.CreateBranch(branch);
           
            return new JsonResult { Data = "Registered", JsonRequestBehavior = JsonRequestBehavior.AllowGet };

        }


The Structure map Controller factory were configured properly and once this method is commented everything working except when this post method was called the error was thrown.

Reason why it threw the exception was that the class mBranch had only once constructor with a parameter when the method is called mBranch is instantiated it there was no constructor without parameter and resulted in this error.

public class mBranch
    {
        public mBranch(int companyId)
        {
            CompanyID = companyId;

            BranchContact = new mContactInfo();
        }


             //error thrown due to this constructor was missing
        public mBranch()
        {
            BranchContact = new mContactInfo();
        }

      }

Friday, January 13, 2012

Single or multiple Image upload on ASP.NET MVC3

I spent a great time searching for an example of Upload Image on ASP.NET MVC3 web site.

These are the two simple steps you need to do. (single image)

Create the view
---------------------

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="firstName" id="firstName" />
<input type="file" name="file" id="file1" />
<input type="submit" />

*Note - Make sure the name that you give for the input type file is important

or If u are using html helper to create the form

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="text" name="firstName" id="firstName" />
    <input type="file" name="file" id="file1" />
    <input type="submit" />
}

Create the controller
[HttpPost]
        public ActionResult Upload(string firstName, HttpPostedFileBase file)
        {            
                if (file.ContentLength > 0)
                {
                    using(Image img= Image.FromStream(file.InputStream))
                    {
                        //--Initialize the size of the array
                        byte[] imgData = new byte[file.InputStream.Length];

                        //--Create a new BinaryReader and set the InputStream
                        //-- for the Images InputStream to the
                        //--beginning, as we create the img using a stream.
                        BinaryReader reader = new BinaryReader(file.InputStream);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        //--Load the image binary.
                        imgData = reader.ReadBytes((int)file.InputStream.Length);

                        //--Create a new image to be added to the database
                    }
                }
            return RedirectToAction("Index");
        }     

For multiple Image upload 

The View
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="firstName" id="firstName" />
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />   
<input type="submit" />


or If u are using html helper to create the form

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="text" name="firstName" id="firstName" />
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="submit" />
}

The Controller

[HttpPost]
        public ActionResult Upload(string firstName, IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    using(Image img= Image.FromStream(file.InputStream))
                    {
                        //--Initialize the size of the array
                        byte[] imgData = new byte[file.InputStream.Length];

                        //--Create a new BinaryReader and set the InputStream
                        //-- for the Images InputStream to the
                        //--beginning, as we create the img using a stream.
                        BinaryReader reader = new BinaryReader(file.InputStream);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        //--Load the image binary.
                        imgData = reader.ReadBytes((int)file.InputStream.Length);

                        //--Create a new image to be added to the database
                    }
                }
            }
            return RedirectToAction("Index");
        }  

Reference
http://garfbradazweb.wordpress.com/2011/08/16/mvc-3-upload-sql-server-entity-framework/