使用Ajax MVC上传图像

时间:2016-12-08 12:39:58

标签: ajax asp.net-mvc

我正在尝试使用Ajax.BeginForm上传图像,但在我的控制器中,文件显示为null。 我的代码是:

@using (Ajax.BeginForm("Create", "PriceManager", new AjaxOptions
            {
                HttpMethod = "Post",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "PriceList",
                OnSuccess = "ClearInputField",
                OnFailure = "Fail",
                OnComplete = "Complete"


            }, new { enctype= "multipart/form-data" }))

我甚至尝试过使用Html.Begin:

 @{ 
        var attributes = new Dictionary<string, object>();


        attributes.Add("data-ajax-complete ", "Complete");
        attributes.Add("data-ajax-mode", "replace");
        attributes.Add("data-ajax-update ", "#PriceList");
        attributes.Add("id", "form0");
        attributes.Add("enctype", "multipart/form-data");
        attributes.Add("data-ajax", "true");
    }
@using (Html.BeginForm("Create", "ProductManager", FormMethod.Post,attributes))

文件值仍然为空。知道我在这里做错了什么。 感谢

1 个答案:

答案 0 :(得分:2)

试试这个:

    @using (Ajax.BeginForm("About", "Home", new AjaxOptions
{
    HttpMethod = "Post",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "PriceList",
    OnSuccess = "ClearInputField",
    OnFailure = "Fail",
    OnComplete = "Complete"


}, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="flResim" />

    <input type="submit" value="send" />
}

我尝试过并且工作过。