Asp.net mvc 在Asp.net mvc控制器中上载图像
我有一个上传表单,我想传递我的信息,如图像和其他字段,但我不知道如何上传图像 这是我的模型课Asp.net mvc 在Asp.net mvc控制器中上载图像,asp.net-mvc,razor,file-upload,httppostedfilebase,Asp.net Mvc,Razor,File Upload,Httppostedfilebase,我有一个上传表单,我想传递我的信息,如图像和其他字段,但我不知道如何上传图像 这是我的模型课 公共部分类NewProductCategory { 公共字符串ProductName{get;set;} 公共字符串ProductDescription{get;set;} 公共字符串ProductPrice{get;set;} 公共字符串ProductImage{get;set;} 公共字符串ProductQuantity{get;set;} 公共可为空的ProductStatus{get;s
公共部分类NewProductCategory
{
公共字符串ProductName{get;set;}
公共字符串ProductDescription{get;set;}
公共字符串ProductPrice{get;set;}
公共字符串ProductImage{get;set;}
公共字符串ProductQuantity{get;set;}
公共可为空的ProductStatus{get;set;}
公共可空类别ID{get;set;}
公共HttpPostedFileBase用户\图像\数据{get;set;}
公共虚拟类别{get;set;}
}
更新Answer
@model MvcApplication1.Models.NewProductCategory
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm("Create", "Temp", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ @Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductCategory</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProductName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.ProductName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductDescription)
@Html.ValidationMessageFor(model => model.ProductDescription)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductPrice)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductPrice)
@Html.ValidationMessageFor(model => model.ProductPrice)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.user_image_data)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.user_image_data, new { Type = "File" })
@Html.ValidationMessageFor(model => model.user_image_data)
</div>
<div class="editor-label">
@Html.Label("Upload your image")
</div>
<div class="editor-label">
@Html.TextBox("file",null,htmlAttributes: new { Type = "file" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductQuantity)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductQuantity)
@Html.ValidationMessageFor(model => model.ProductQuantity)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductStatus)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductStatus)
@Html.ValidationMessageFor(model => model.ProductStatus)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId, "Category")
</div>
<div class="editor-field">
@Html.DropDownList("CategoryId", String.Empty)
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@model mvcapapplication1.Models.NewProductCategory
@{
ViewBag.Title=“创建”;
}
@使用(Html.BeginForm(“Create”、“Temp”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
产品类别
@Html.LabelFor(model=>model.ProductName)
@Html.EditorFor(model=>model.ProductName)
@Html.ValidationMessageFor(model=>model.ProductName)
@LabelFor(model=>model.ProductDescription)
@EditorFor(model=>model.ProductDescription)
@Html.ValidationMessageFor(model=>model.ProductDescription)
@LabelFor(model=>model.ProductPrice)
@EditorFor(model=>model.ProductPrice)
@Html.ValidationMessageFor(model=>model.ProductPrice)
@LabelFor(model=>model.user\u image\u数据)
@Html.TextBoxFor(model=>model.user\u image\u数据,新的{Type=“File”})
@Html.ValidationMessageFor(model=>model.user\u image\u数据)
@Html.Label(“上传图像”)
@TextBox(“file”,null,htmlAttributes:new{Type=“file”})
@LabelFor(model=>model.ProductQuantity)
@EditorFor(model=>model.ProductQuantity)
@Html.ValidationMessageFor(model=>model.ProductQuantity)
@LabelFor(model=>model.ProductStatus)
@EditorFor(model=>model.ProductStatus)
@Html.ValidationMessageFor(model=>model.ProductStatus)
@LabelFor(model=>model.CategoryId,“Category”)
@Html.DropDownList(“CategoryId”,String.Empty)
@Html.ValidationMessageFor(model=>model.CategoryId)
}
因为您绑定到一个名为user\u image\u data
(不是file
)的属性,但是您没有显示您的模型,所以我们不知道它是什么-它的类型应该是HttpPostedFileBase
。如果要绑定到名为file
的参数,请将我使用HttpPostedFileBase完成的输入命名为file
Ya,但是我有一个问题,@model mvcapapplication1.Models.NewProductCategory如果没有这个,它可以运行,但是它有另一个输入字段,它会出错,我会编辑并添加我的模型类。我已经做了上述更改,仍然在httpHttpPostedFileBase文件中获得空值,现在还有什么其他选项,我只是在没有模型类的情况下创建了一个演示,但在添加和传递(NewProductCategory productcategory,HttpPostedFileBase file)时,该类的值为空