Asp.net mvc 4 Asp.net MVC…HttpPostedFileBase uploadImage不总是空的

Asp.net mvc 4 Asp.net MVC…HttpPostedFileBase uploadImage不总是空的,asp.net-mvc-4,Asp.net Mvc 4,我想上传一张图片,但它总是为空。我正在使用HttpPostedFileBase 这是我的控制器 public ActionResult EmployeeDetail(EmployeeModel employee, HttpPostedFileBase UploadImage)//this UploadImage Object is always null { EmployeeModel employeeModel = new EmployeeModel(); if (stri

我想上传一张图片,但它总是为空。我正在使用
HttpPostedFileBase

这是我的控制器

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>
}

我看不到您正在向EmployeeDetail()传递任何参数。你能为你的雇员模型获取数据吗?如果是,则至少确认您的视图能够调用EmployeeDetail()操作

接下来,您需要确保向EmployeeDetail()传递了正确的参数。我可以想到的一种方法是使用ajax。因此,您可以在单击submit按钮时创建一个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!");
     }
}
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!");
     }
}