Search code examples
javascriptc#htmlasp.net-mvchttppostedfilebase

Pass Data from view to controller with file name form file input tag in mvc


I have one mvc application , in that i created one form that user will be able to fill it online. in that form i used multiple text boxes and input file tag for user's photo . i also created model , first see this Sampleform.cshtml view

@model OnlineFilling.Models.ConsumerModel


            <div class="row">
              <div class="row" style="width:90%; margin-left:45px">

                      <div class="row">
    <div class="row">
                                        <div class="row" >
                                           <div class="col-sm-3"><h4>Consumer Name:</h4> </div> 
                                                    <div class="col-sm-2"><input type="text" class="input-sm" id="txtConsumerFirstName" placeholder="First Name" style="width:200px;"/></div>
                                           </div>
                                         <br />
                                        <div class="row">
                                                    <div class="col-sm-15">
                                                    <div class="col-sm-3"><h4>Father/Husband/WifeName:</h4> </div>
                                          <div class="col-sm-2"><input type="text" class="input-sm"  id="txtParentsFirstName" placeholder="First Name"  style="width:200px"/></div>
                                        </div>
                                        <br />
               <div class="row">
                                                    <div class="col-sm-15">
                                                    <div class="col-sm-3"><h4>Mother Name:</h4></div> 
                                                    <div class="col-sm-2"><input type="text" class="input-sm"  id="txtMotherFirstName" placeholder="First Name"  style="width:200px"/></div>
                                         </div>
                                        <br />
                                <div class="row">
                                                    <div class="col-sm-15">
                                                    <div class="col-sm-3"><h4>Upload photo And Signature</h4></div>
                                                        <div class="col-sm-3">
                                                            <fieldset class="scheduler-border" style="background-color:#fff">
                                                            <legend class="scheduler-border">Consumer Photo</legend>
                                                            <input type="file" id="ConsumerPic" value="Photo" onchange="getfilename(this);"/>
                                                            </fieldset>
                                                        </div>
                                                        <div class="col-sm-3">
                                                            <fieldset class="scheduler-border" style="background-color:#fff">
                                                            <legend class="scheduler-border">Consumer Sign</legend>
                                                            <input type="file" id="ConsumerSign" value="Signature" />
                                                            </fieldset>
                                                        </div>
                                                    </div>
                                        </div>
                                        <br />
                                        .
            .
            .
            .                                            
                                        <div class="row">
                                                    <div class="col-sm-2"><h4>Account No</h4></div>
                                                    <div class="col-sm-2"><input type="text" id="txtAccount"  class="input-sm" /></div>
                                                    <div class="col-sm-1"> <h4>Bank I.f.S Code</h4></div>
                                                    <div class="col-sm-2"><input type="text" id="txtIFS"  class="input-sm" /></div>

                                        </div>
                               </div>
                            </div>
             </div>   
                  </div>               
             </form>

then this is my HomeController

[HttpPost]
    public ActionResult SampleForm(ConsumerModel objModel, int ConID,  string consumerfN, string ConGaurdian, string ConMother, int AcntNum, string conIfS)
    {
        var ImgfileName = Path.GetFileName(objModel.file1.FileName);
        WebImage img = new WebImage(ImgfileName);
        string imageName = ConID + "_" + consumerfN + "_" + conCity+"_ProfPic";
        var path = Path.Combine(Server.MapPath("~/FileManager"), imageName);
        if (img.Width > 450 || img.Width<450)
        {
            img.Resize(450, 450);
            img.Save(path);
        }
        img = new WebImage(ConSign.InputStream);
        string SignName = ConID + "_" + consumerfN + "_" + conCity + "Signture";
        var SignPath = Path.Combine(Server.MapPath("~/FileManager"), imageName);
        if (img.Width > 800 || img.Height > 300)
        {
            img.Resize(800, 300);
            img.Save(SignPath);
        }

        consumerModel.ConsumerFName = consumerfN;
        consumerModel.GaurdianName = ConGaurdian;
        consumerModel.MotherName = ConMother;

        consumerModel.AccountNo = AcntNum;
        consumerModel.IFScode = conIfS;

        consumerModel.PhotoPath = path;
        consumerModel.Signature = SignPath;

        bool TotalInfo = Middleclass.GetConsumerInfo(consumerModel);
        return RedirectToAction("SampleForm");
    }

this one is my model

 public class ConsumerModel
{
    public int Id { get; set; }
    public string ConsumerFName { get; set; }
    public string ConsumerMName { get; set; }
    public string ConsumerLName { get; set; }
    public string GaurdianName { get; set; }
    public string MotherName { get; set; }
    public string Qualification { get; set; }
    public System.DateTime DOB { get; set; }
    public int Age { get; set; }
    public string MarriedStatus { get; set; }
    public string BloodGroup { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string District { get; set; }
    public string State { get; set; }
    public int PINcode { get; set; }
    public Nullable<int> Telephone { get; set; }
    public int Mobile { get; set; }
    public string E_Mail { get; set; }
    public string PANno { get; set; }
    public string AdharNumber { get; set; }
    public string AvailableOn { get; set; }
    public int MobileNumberAv { get; set; }
    public string CompanyGrp { get; set; }
    public int AccountNo { get; set; }
    public string IFScode { get; set; }
    public string BankAddr { get; set; }
    public string PhotoPath { get; set; }
    public string Signature { get; set; }
    public string Gender { get; set; }
    public string Nationality { get; set; }
    public HttpPostedFileBase file1 { get; set; }
    public HttpPostedFileBase file2 { get; set; }
}

but finally i used ajax call to send data it will execute correctly but it not sent file tag value to controller, then i used

@using (Html.BeginForm("Sampleform", "home", FormMethod.Post, new {enctype="multipart/form-data"}))

then it will raise this error like.. enter image description here

so please can you suggest me to solve this error...........??????


Solution

  • I hope your solved your problem since July, but I post my answer for the next ones :

    I solved this problem by adding a name tag at the input field.

    try :

     <input type="file" id="ConsumerPic" value="Photo" name="file" onchange="getfilename(this);"/>
    

    instead of :

     <input type="file" id="ConsumerPic" value="Photo" onchange="getfilename(this);"/>
    

    I hope this will help some :)