I wanted create a form where user will edit there profile and upload an image if they want, Here is my View
//Form String @using(Html.BeginForm("EditProfile","Employee",FormMethod.Post, 
 new { enctype = "multipart/form-data " }))
//Input HTML  <input type="file" name="file" id="file"/>    
//Submit<input type="submit" name="SubmitBtn" value="SaveProfile" />
                        <input type="submit" name="SubmitBtn" value="Cancel" />   
Now My Controller
    //Profile Modification
    [Authorize]
    [HttpPost]
    public ActionResult EditProfile(employer model, string SubmitBtn,HttpFileCollection file)
    {
        switch (SubmitBtn)
        {
            case "SaveProfile":
                try
                {
                    if (ModelState.IsValid)
                    {
                          //the file is always null
                        if (file != null)
                        {
                             //Function I want to Apply on file
                             model.logoname = logouploded(file);  
                            return RedirectToAction("Profile", "Employer");
                        }
                        return RedirectToAction("EditProfile");
                    }
                    //ChangeEmployeeProfile(model);
                }
                catch (Exception ex)
                {
                    ViewBag.warn = ex.Message;
                    return View(model);
                }
                break;
            case "Cancel":
                return RedirectToAction("index");
        }
        return View(model);
    }
Now no matter what file I upload file is always coming null I have also tried HttpFileCollectionBase as parameter in Action Function still file was null
Just to mention Model of Employee contains only image file name because I only want to save image name in database.
Change HttpFileCollection type to HttpPostedFileBase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With