Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to implement simple file upload

C#
public class Movie
   {
       public int id { get; set; }
       public string name { get; set; }
       public string duration { get; set; }
   }



My View

C#
@model MvcApplication1.Models.Movie
@{
    ViewBag.Title = "FileUpload";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()   
    
    <fieldset>
        <legend>Movie</legend>
        <div>
            @Html.LabelFor(m=>m.name)
            @Html.TextBoxFor(m=>m.name)
        </div>
        <div>
            @Html.LabelFor(m=>m.duration)
            @Html.TextBoxFor(m=>m.duration)
        </div>

        <div>
            <input type="file" name="fileUpload" />
        </div>

        <p>
            <input type="submit" value="Upload movie" />
        </p>
    </fieldset> 
}


C#
[HttpGet]
        public ActionResult FileUpload()
        {           
            return View();
        }

        [HttpPost]
        public ActionResult FileUpload(Movie OMovie, HttpPostedFile postedFile)
        {
            var file = postedFile;
            return Content("You got the file.");
        }


But i got error

VB
[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232



please help...
Posted
Updated 5-May-14 20:38pm
v2

 
Share this answer
 
Comments
Vi(ky 31-Mar-14 5:28am    
I already tried this but problem remain same

In my case i used

public Movie(string name)
{
this.name = name;
}
vsrikanth87 31-Mar-14 5:47am    
cn u paste the complete code of ur view
Vi(ky 31-Mar-14 6:07am    
i already pasted the code of view
ashok rathod 31-Mar-14 6:08am    
i think there could be problem with specification of model properties as they are not decorated with the datamember attribute..
i can suggest your movie class should be like this.

[DataContract]
[Serializable]
<pre lang="cs">public class Movie
{
[FataMember]
public int id { get; set; }
<pre lang="cs">[DataMember]
public string name { get; set; }
[DataMember]
public string duration { get; set; }
}</pre></pre>
To avoid this, Use any dependency Resolver, because if we use parameterized constructor then dependency injection arises. SO to avoid that use IOC(Inversion of Control)/StructureMap3 from nuget. Else you can use ninject/unity.
Hope this helps
Thanks.
 
Share this answer
 
 
Share this answer
 
Comments
Sanket Saxena 20-Aug-15 0:54am    
Check the date men..u are answering to an 1+ year old post :)
Hi,

Most probably you might have parameterized constructor in your controller and whatever dependency resolver you are using is not able to resolve the dependency properly. You need to put break-point where the dependency resolver method is written and you will get the exact error in inner exception.
 
Share this answer
 
Comments
This is an old question man.
To avoid this issue try to make all constructors public that is if you define a constructor in the Repository or Controller or use any dependency Revolver.
 
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