weixin_33676492 2016-12-08 12:39 采纳率: 0%
浏览 31

使用Ajax MVC上传图像

I am trying to upload image using Ajax.BeginForm but in my controller the file shows null. My code is:

@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" }))

I even tried using 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))

Still the file value remain null. Any idea what I am doing wrong here. Thanks

  • 写回答

1条回答 默认 最新

  • weixin_33694620 2016-12-09 07:29
    关注

    Try this:

        @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" />
    }
    

    I tried and worked.

    评论

报告相同问题?