Pergunta

We have very simple form with file input. When we post form we can handle other element from model. But file input always return null. By the way we use our view and controller in Telerik Sitefinity as a custom control. It may related with this because we couldn't find any solution.

Here is view code

@using (Html.BeginForm("Index", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form">
    <dl>
        <dd>
            @Html.LabelFor(model => model.ShareRecipe.Name)
            @Html.EditorFor(model => model.ShareRecipe.Name)
        </dd>
        <dd>
            @Html.LabelFor(model => model.ShareRecipe.EmailAddress)
            @Html.EditorFor(model => model.ShareRecipe.EmailAddress)
        </dd>
        <dd>
            @Html.LabelFor(model => model.ShareRecipe.RecipeArea)
            @Html.EditorFor(model => model.ShareRecipe.RecipeArea)
        </dd>
        <dd>
            <input type="file" name="fileUpload" id="fileUpload" />
        </dd>
        <dd class="last">
            <input type="submit" class="btn btn-danger" onclick="return ValidateForm();" value="Send" /></dd>
    </dl>
</div>
}  

Here is our controller side.

        [HttpPost]
        public ActionResult Index(ShareRecipeModel model, HttpPostedFileBase fileUpload, FormCollection values)
        {
                if (fileUpload != null && fileUpload.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(fileUpload.FileName);
                    var path = Path.Combine(Server.MapPath("~/img/"), fileName);
                    fileUpload.SaveAs(path);
                    model.ImageUrl = fileName;
                }
}

We also try to post with basic form tag. like:

<form method="post" enctype="multipart/form-data"></form>

it's not working. Also in Html.BeginForm we tried all variations index and controller name like:

null,null
"Index","ShareRecipeController"

etc.

At the end I'd like give information about using custom control in Telerik Sitefinity we have this line on our controller:

[ControllerToolboxItem(Name = "ShareRecipe", Title = "Share Your Recipe", SectionName = "MvcWidgets")]
public class ShareRecipeController : BaseController

It can be great for us if anyone have any idea, Thanks in advance, Serhad.

Foi útil?

Solução

In our form tags we have necessary element which is "FormMethod.Post, new { enctype = "multipart/form-data" }" but because of sitefinity structure, when we use our custom control as custom user control in page. It goes related master page of that page. The form tag in master page effects our form tag in custom control.

To avoid this problem add "method="post" enctype="multipart/form-data" properties in your master page tag instead of adding custom control view.

ref: http://www.sitefinity.com/developer-network/forums/bugs-issues-/upload-file-with-mvc

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top