【发布时间】:2016-09-26 10:42:03
【问题描述】:
我有一个表单,我要发布 2 个不同的文件,这些文件属于我的 Db 表中的 2 个不同的值。
例如。 file1=user image , file2=user company logo.
所以我需要使用我的viewModel 将文件 url 附加到它的 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)
{
【问题讨论】:
-
我看不到任何上传图像或将数据保存在数据库中的内容。文件网址是什么意思?图片可以用文件名或字节数组保存。
-
字符串 savedFileName = System.IO.Path.GetFileName(file.FileName); var newFileName = DateTime.Now.ToString("yyMMddmmss") + savedFileName; if (System.IO.File.Exists(path)) 继续; MyFileName = ("/Images/RImages/" + newFileName); & newFileName = Server.MapPath(MyFileName);文件。另存为(新文件名); & 然后 Image1=MyfileName;并添加保存等
-
你已经硬编码了。我将提供一个带有两个文件上传的样本。很简单。
-
没关系延迟。我已经更新了我的帖子。立即查看并告诉我。
标签: asp.net-mvc asp.net-mvc-5 asp.net-mvc-viewmodel