Fahad
Fahad

Reputation: 173

Image upload using 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

Upvotes: 0

Views: 544

Answers (1)

hakantopuz
hakantopuz

Reputation: 511

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.

Upvotes: 2

Related Questions