1

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.

4

1 回答 1

2

在我们的表单标签中,我们有必要的元素“FormMethod.Post, new { enctype = "multipart/form-data" }" 但由于站点有限结构,当我们在页面中使用自定义控件作为自定义用户控件时。它转到该页面的相关母版页。母版页中的表单标签会影响我们在自定义控件中的表单标签。

为避免此问题,请在母版页标记中添加“method="post" enctype="multipart/form-data" 属性,而不是添加自定义控件视图。

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

于 2013-05-14T12:57:38.703 回答