【发布时间】:2013-05-07 12:26:47
【问题描述】:
我们有非常简单的文件输入表单。当我们发布表单时,我们可以处理模型中的其他元素。但是文件输入总是返回null。 顺便说一句,我们在 Telerik Sitefinity 中使用我们的视图和控制器作为自定义控件。这可能与此有关,因为我们找不到任何解决方案。
这里是查看代码
@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>
}
这是我们的控制器端。
[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;
}
}
我们也尝试使用基本的表单标签发布。喜欢:
<form method="post" enctype="multipart/form-data"></form>
它不工作。 同样在 Html.BeginForm 我们尝试了所有变体索引和控制器名称,例如:
null,null
"Index","ShareRecipeController"
等等
最后,我想提供有关在 Telerik Sitefinity 中使用自定义控件的信息 我们的控制器上有这一行:
[ControllerToolboxItem(Name = "ShareRecipe", Title = "Share Your Recipe", SectionName = "MvcWidgets")]
public class ShareRecipeController : BaseController
如果有人有任何想法,这对我们来说会很棒, 提前致谢, 塞尔哈德。
【问题讨论】:
标签: c# asp.net-mvc-3 file-upload telerik sitefinity-5