2015-09-27 28 views
2

我想上传一张图片,但它总是变为空。我正在使用HttpPostedFileBaseAsp .net MVC ... HttpPostedFileBase uploadImage并非总是为空

这是我的控制器

public ActionResult EmployeeDetail(EmployeeModel employee, HttpPostedFileBase UploadImage)//this UploadImage Object is always null 
{ 

    EmployeeModel employeeModel = new EmployeeModel(); 

    if (string.IsNullOrEmpty(employeeModel.Name)) 
    { 
     ModelState.AddModelError("Name", "Name is Required"); 
    } 
    employeeModel.Name = employee.Name; 
    if (string.IsNullOrEmpty(employeeModel.DOJ)) 
    { 
     ModelState.AddModelError("DOJ", "DOJ is Requird"); 
    } 
    employeeModel.DOJ = employee.DOJ; 
    if (string.IsNullOrEmpty(employeeModel.DOB)) 
    { 
     ModelState.AddModelError("DOB", "DOB is Required"); 
    } 
    employeeModel.DOB = employee.DOB; 
    if (string.IsNullOrEmpty(employeeModel.Designation)) 
    { 
     ModelState.AddModelError("Designation", "Designation is required"); 
    } 
    employeeModel.Designation = employee.Designation; 

    string ImageName = Path.GetFileName(UploadImage.FileName); 
    string Physicalpath = Server.MapPath("~/images/" + ImageName); 
    UploadImage.SaveAs(Physicalpath); 
    employee.UploadImage = Physicalpath; 

    //string ImageName = Path.GetFileName(image.FileName); 
    //string physicalPath = Server.MapPath("~/images/" + ImageName); 
    //image.SaveAs(physicalPath); 


    //  ModelState.AddModelError("UploadImage", "upload is required"); 
    //employee.UploadImage = physicalPath; 
     EmployeeBusinessLayer employeeBL = new EmployeeBusinessLayer(); 
     employeeBL.InsertDataRegistration(employeeModel); 

    return RedirectToAction("Index"); 
} 

这是我的看法

@using (Html.BeginForm("EmployeeDetail", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @data_ajax = "false" })) //i have used all the codes which could be need to make it work...still not working 
{ 

<div class="MainDiv"> 
    <table class="Table"> 
     <tr class="Row"> 
      <td class="Column1"> Name</td> 
      <td class="Column2">@Html.TextBoxFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)</td> 
     </tr> 
     <tr class="Row"> 
      <td class="Column1">DOJ </td> 
      <td class="Column2">@Html.TextBoxFor(model => model.DOJ, new { @class = "datepicker", autocomplete = "off" }) @Html.ValidationMessageFor(model => model.Name) </td> 
     </tr> 

     <tr class="Row"> 
      <td class="Column1">DOB</td> 
      <td class="Column2">@Html.TextBoxFor(model => model.DOB, new { @class = "datepicker", autocomplete = "off" }) @Html.ValidationMessageFor(model => model.Name)</td> 
     </tr> 
     <tr class="Row"> 
      <td class="Column1">DESIGNATION</td> 
      <td class="Column2">@Html.TextBoxFor(model => model.Designation) @Html.ValidationMessageFor(model => model.Name)</td> 
     </tr> 
     <tr class="Row"> 
      <td class="Column1">UPlOAD </td> 
      <td class="Column2">@Html.TextBoxFor(model => model.UploadImage, new { @type = "File" }) 
      </td> 
     </tr> 
     <tr class="Row"> 
      <td colspan="2"> 
       <input type="submit" class="button" name="submit" value="Submit"> 
       <input type="reset" class="button1" value="Clear" name="Clear"> 
      </td> 
     </tr> 

    </table> 
    <script src="~/Scripts/jquery-ui-1.9.2.custom/development-bundle/jquery-1.8.3.js"></script> 
    <script src="~/Scripts/jquery-ui-1.9.2.custom/development-bundle/ui/minified/jquery-ui.custom.min.js"></script> 

    <script type="text/javascript"> 
     $(function() { 
      // This will make every element with the class "date-picker" into a DatePicker element 
      $('.datepicker').datepicker(); 
     }) 
    </script> 
</div> 
} 

这是我的模型

public Model 
{ 
    public int EmployeeId { get; set; } 
    [Required(ErrorMessage = "this is required")] 
    public string Name { get; set; } 
    [Required (ErrorMessage = "This is required")] 
    public string DOJ { get; set; } 
    [Required(ErrorMessage ="This is required")] 
    public string DOB { get; set; } 
    [Required(ErrorMessage ="This is required")] 
    public string Designation { get; set; } 
    [Required(ErrorMessage = "This is required")] 
    public string UploadImage { get; set; } 
    public HttpPostedFileBase MyFile { get; set; } 
} 

回答

0

我没有看到任何你正在传递任何参数EmployeeDetail ()。你能为你的EmployeeModel获取数据吗?如果是,那么至少要确认你的视图能够调用EmployeeDetail()动作。

接下来,您需要确保将正确的参数传递给EmployeeDetail()。我能想到的一种方法是使用ajax。因此,您可以在单击提交按钮时创建ajax调用,并将所有数据和上传的文件输入以ajax方法传递。

这是使用AJAX调用与jQuery语法数据传递给动作

var inputFiles = $('inpFile').val(); 
var actMethod = "@Url.Action("EmployeeDetail", "Index")" 
var postData = { 
    "Name": $('inpName').val(), 
    "DOJ": $('inpDOJ').val(), 
    ... 
    "UploadImage": inputFiles 
} 
$.ajax() 
{ 
    url: actMethod , 
    data: postData, 
    type: "POST", 
    success: function (data) { 
     alert("Insert Successful!"); 
    } 
} 
的一个例子