Ajax asp.net mvc 5 中的上传助手没有工作并获得空值
Ajax Upload helper in asp.net mvc 5 didn't work and gets Null value
我想使用 Upload control
在 asp.net mvc 5
中使用 Razor engine
保存图像。这些是我下面的代码,我认为一切都很好,但我不知道为什么 controller
中的 UploadImage
得到 null 并且没有得到选定的图像。有人可以帮我吗?
管理员控制器
根据一篇文章,我在控制器参数中使用了相同的名称 @html.upload("UploadImage")
。
[HttpPost]
public ActionResult AddSubGood(SubGood subgood, HttpPostedFileBase UploadImage)
{
var MainGoodId = subgood.FKMainGoodID;
SubGoodRepositories blSubGood = new SubGoodRepositories();
if (ModelState.IsValid)
{
subgood.FKMainGoodID = MainGoodId;
if (blSubGood.Add(subgood))
{
return MessageBox.Show("added successfully", MessageType.Success);
}
else
{
return MessageBox.Show(" didn't add", MessageType.Error);
}
}
else
{
return MessageBox.Show(ModelState.GetErrors(), MessageType.Warning);
}
}
AddSubGood.cshtml
根据一篇文章,我将enctype="multipart/form-data"
添加到form
@using (Ajax.BeginForm("Admin", "AddSubGood", new AjaxOptions { HttpMethod = "Post", Url = "/Admin/AddSubGood" }, new{enctype="multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="form-group">
<div class="col-md-10">
@Html.Upload("UploadImage")
@Html.ValidationMessageFor(model => model.SubGoodURL)
</div>
@Html.LabelFor(model => model.SubGoodURL, new { @class = "control-label col-md-2" })
</div>
}
UploadHelper.cs
public static class UploadHelper
{
public static MvcHtmlString Upload(this HtmlHelper helper, string name, object htmlAttributes = null)
{
TagBuilder input = new TagBuilder("input");
input.Attributes.Add("type", "file");
input.Attributes.Add("id", helper.ViewData.TemplateInfo.GetFullHtmlFieldId(name));
input.Attributes.Add("name", helper.ViewData.TemplateInfo.GetFullHtmlFieldName(name));
if (htmlAttributes != null)
{
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
input.MergeAttributes(attributes);
}
return new MvcHtmlString(input.ToString());
}
public static MvcHtmlString UploadFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes = null)
{
//helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression))
var data = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
TagBuilder input = new TagBuilder("input");
input.Attributes.Add("type", "file");
input.Attributes.Add("id", helper.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)));
input.Attributes.Add("name", helper.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
if (htmlAttributes != null)
{
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
input.MergeAttributes(attributes);
}
return new MvcHtmlString(input.ToString());
}
}
不支持使用Ajax.BeginForm
上传文件。如果您改用 Html.BeginForm
它将起作用。如果你要 ajax post 你可以使用一些文件上传插件,比如 dropzonejs or Blueimp File Upload.
我想使用 Upload control
在 asp.net mvc 5
中使用 Razor engine
保存图像。这些是我下面的代码,我认为一切都很好,但我不知道为什么 controller
中的 UploadImage
得到 null 并且没有得到选定的图像。有人可以帮我吗?
管理员控制器
根据一篇文章,我在控制器参数中使用了相同的名称 @html.upload("UploadImage")
。
[HttpPost]
public ActionResult AddSubGood(SubGood subgood, HttpPostedFileBase UploadImage)
{
var MainGoodId = subgood.FKMainGoodID;
SubGoodRepositories blSubGood = new SubGoodRepositories();
if (ModelState.IsValid)
{
subgood.FKMainGoodID = MainGoodId;
if (blSubGood.Add(subgood))
{
return MessageBox.Show("added successfully", MessageType.Success);
}
else
{
return MessageBox.Show(" didn't add", MessageType.Error);
}
}
else
{
return MessageBox.Show(ModelState.GetErrors(), MessageType.Warning);
}
}
AddSubGood.cshtml
根据一篇文章,我将enctype="multipart/form-data"
添加到form
@using (Ajax.BeginForm("Admin", "AddSubGood", new AjaxOptions { HttpMethod = "Post", Url = "/Admin/AddSubGood" }, new{enctype="multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="form-group">
<div class="col-md-10">
@Html.Upload("UploadImage")
@Html.ValidationMessageFor(model => model.SubGoodURL)
</div>
@Html.LabelFor(model => model.SubGoodURL, new { @class = "control-label col-md-2" })
</div>
}
UploadHelper.cs
public static class UploadHelper
{
public static MvcHtmlString Upload(this HtmlHelper helper, string name, object htmlAttributes = null)
{
TagBuilder input = new TagBuilder("input");
input.Attributes.Add("type", "file");
input.Attributes.Add("id", helper.ViewData.TemplateInfo.GetFullHtmlFieldId(name));
input.Attributes.Add("name", helper.ViewData.TemplateInfo.GetFullHtmlFieldName(name));
if (htmlAttributes != null)
{
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
input.MergeAttributes(attributes);
}
return new MvcHtmlString(input.ToString());
}
public static MvcHtmlString UploadFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes = null)
{
//helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression))
var data = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
TagBuilder input = new TagBuilder("input");
input.Attributes.Add("type", "file");
input.Attributes.Add("id", helper.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)));
input.Attributes.Add("name", helper.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
if (htmlAttributes != null)
{
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
input.MergeAttributes(attributes);
}
return new MvcHtmlString(input.ToString());
}
}
不支持使用Ajax.BeginForm
上传文件。如果您改用 Html.BeginForm
它将起作用。如果你要 ajax post 你可以使用一些文件上传插件,比如 dropzonejs or Blueimp File Upload.