【发布时间】:2018-01-31 18:16:58
【问题描述】:
我是 ASP.Net(以及一般的 webdev)的新手,我正在尝试实现一个提供类似 CV 功能的网站。其中之一是上传和查看一个人的简历照片。使用脚手架和在SO: Uploading/Displaying Images in MVC 4 找到的代码示例,我设法提出了以下代码,其中有问题的部分被更大的空间包围:
<div class="form-horizontal">
<h4>Author</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@using (Html.BeginForm("FileUpload", "CV", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" style="width: 100%;" />
<br/>
<input type="submit" value="Upload" class="submit" />
}
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MobileNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MobileNum, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MobileNum, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LinkedIn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LinkedIn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LinkedIn, "", new { @class = "text-danger" })
</div>
</div>
这是负责上传的视图的相关部分,而我要处理请求的Controller CV 中的方法是这样的:
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
if (file != null)
{
string pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
Server.MapPath("~/Content/Images"), pic);
// file is uploaded
file.SaveAs(path);
// save the image path path to the database or you can send image
// directly to database
// in-case if you want to store byte[] ie. for DB
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
//now I only have 1 mock author
db.Authors.FirstOrDefault().Picture = array;
}
}
// after successfully uploading redirect the user
return View();
}
我的问题是,尽管指定了 Action 和 Controller 名称,并且 VS 能够将 View 连接到 Controller,但由于某种原因,该帖子从未使用此方法(至少它会滑动到Go To Controller 命令上的相应命令)。我确定这是一些新手错误,但我似乎无法找到原因。
【问题讨论】:
-
您显示的代码应该可以正常工作。你的 html 看起来很奇怪(在
<div class="form-group">中有一个<form>所以最好的猜测是你有嵌套的表单,它是无效的 html 并且不受支持 -
谢谢,这是问题的根源。如果您可以将其转换为带有某种解释的答案,我很乐意接受它作为答案。
-
您需要编辑问题以显示外部形式(否则答案将没有多大意义)
-
我添加了更多代码
标签: c# asp.net-mvc