我有一个表单,我在我的Db表中发布了2个不同的文件属于2个不同的值。
EG。 file1=user image
,file2=user company logo
。
所以我需要将文件url附加到我的viewModel
的db值,
像这样:(永远不会工作)
public ActionResult Create(LectureFormViewModel viewModel)
{
foreach ((string item in Request.Files).viewModel.Image1)
{
//Do
}
foreach ((string item in Request.Files).viewModel.Image2)
{
//Do
}
var lecture = new Lecture
{
Image1 = xxx,
Image2=yyy,
}
_context.LectureGigs.Add(Lecture);
}
我的ViewModel(我已删除参数)
public class LectureFormViewModel
{
public int Id { get; set; }
public byte Genre { get; set; }
public IEnumerable<Genre> Genres { get; set; }
public string Image1 { get; set; }
public string Image2 { get; set; }
public string Action
{
get
{
Expression<Func<LecController, ActionResult>>
update = (c => c.Update(this));
Expression<Func<LecController, ActionResult>>
create = (c => c.Create(this));
var action = (Id != 0) ? update : create;
return (action.Body as MethodCallExpression).Method.Name;
}
}
}
表格(查看)
@using VoosUpW.Models
@model VoosUpW.ViewModels.LectureFormViewModel
@using (Html.BeginForm(Model.Action, "Lec", FormMethod.Post, new { enctype = "multipart/form-data", @id = "abcdefg" }))
{
//parm
<div class="form-group">
@Html.LabelFor(f => f.Image1)
<i class="glyphicon glyphicon-folder-open"></i>
<input id="Image1" name="Image" type="file" class="">
</div>
<div class="form-group">
@Html.LabelFor(f => f.Image2) <i class="glyphicon glyphicon-folder-open"></i>
<input type="file" name="Image2" class="btn btn-default btn-sm btn-google btn-group-justified hvr-shadow " />
</div>
<button type="submit" class="btn btn-primary btn-lg">Save</button>
}
我的动作标题
public ActionResult Create(LectureFormViewModel viewModel)
{
答案 0 :(得分:0)
使用IEnumerable<HttpPostedFileBase>
:
产品:
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ImagePath1 { get; set; }
public string ImagePath2 { get; set; }
public double Price { get; set; }
}
行动:
[HttpPost]
public ActionResult Upload(Product aProduct, IEnumerable<HttpPostedFileBase> files)
{
HttpPostedFileBase file1;
HttpPostedFileBase file2;
using (var context = new DemoEntities()) //DbContext - Database connection
{
file1 = files.ElementAt(0); //Gets the first image
file2 = files.ElementAt(1); //Gets the second image
//Take the first and second file
if (files.ElementAt(0) != null && files.ElementAt(1) != null)
{
if (file1 != null && file1.ContentLength > 0 && file2 != null && file2.ContentLength > 0)
{
var fileName = System.IO.Path.GetFileName(file1.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/Images/"), fileName); //Get the first file path and save to folder
var fileName2 = System.IO.Path.GetFileName(file2.FileName);
string path2 = System.IO.Path.Combine(Server.MapPath("~/Images2/"), fileName2); //Get the second file path and save to folder
/**In database, followings are being saved - Starts**/
aProduct.ProductId = 1002;
aProduct.ImagePath1 = fileName; //Saves the first image file name
aProduct.ImagePath2 = fileName2; //Saves the second image file name
aProduct.ProductName = "Super Product";
aProduct.Price = 2000;
try
{
context.Products.Add(aProduct); //Save the object and context is the dbContext
context.SaveChanges();
}
catch (Exception ex)
{
ex.ToString();
}
/**In database, followings are being saved - Ends**/
}
}
}
return RedirectToAction("Index");
}
注意:不要担心图片路径。为方便起见,只需将文件名保存在Db中,最后在视图中显示,执行以下操作,对于图像上传,您显示的代码将起作用:
<img src="~/Images1/@item.ImagePath1" alt="No Images" class="img">
<img src="~/Images2/@item.ImagePath2" alt="No Images" class="img">