I know I have done some basic mistake, but I can't find solution by myself, I have next form at ASP MVC 4
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "frmMy", data_bind = "submit: onSubmit", action = "/api/MyService/SaveUpload", enctype = "multipart/form-data" }))
{
<button type="submit" class="btn btn-default">Save</button>
}
and web api method
[HttpPost]
[AllowAnonymous]
public async Task<HttpResponseMessage> SaveUpload()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/");
var provider = new MultipartFormDataStreamProvider(root);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
...
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
but after /api/MyService/SaveUpload call my page is make redirect to web api method. How to prevent this behavior and catch method result on page.
This is happening because you are not telling the Beginform method what to do when the call is successful or unsuccessful. There are a few things you can do to correct this.