无法在 mvc 中使用 fileupload 更新图像
我正在尝试更新已更新到数据库的文本和图像。我有一个管理部分,其中有一个新闻菜单,用户可以在其中编辑和更新带有图像的新闻。问题是我可以编辑和更新新闻文本,但图像没有更新。下面是控制器和视图:
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(int id, FormCollection collection, IEnumerable<HttpPostedFileBase> files)
{
INewsRepository newsResp = new NewsRepository();
News news = newsResp.GetNews(id);
if (TryUpdateModel(news)){
newsResp.Save();
return RedirectToAction("Index");
}else{
return View();
}
}
<% using (Html.BeginForm("Edit", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<%: Html.ValidationSummary(true) %>
<table cellpadding="2" cellspacing="2" border="0">
<tr>
<td style="width:100px;">
<div class="editor-label">
Title</div>
</td>
<td>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title, new { style = "width:300px;" })%>
<%: Html.ValidationMessageFor(model => model.Title)%>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="editor-label">
Article content</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Article, new { @class = "tinymce" })%>
<%: Html.ValidationMessageFor(model => model.Article)%>
</div>
</td>
</tr>
<tr>
<td>
Image 1
</td>
<td>
<img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge %>" />
<br />
<input type="file" name="files" id="file1" />
</td>
</tr>
<tr>
<td>
Image 2
</td>
<td>
<img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge2 %>" />
<br />
<input type="file" name="files" id="file2" />
</td>
</tr>
<tr>
<td>
Image 3
</td>
<td>
<img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge3 %>" />
<br />
<input type="file" name="files" id="file3" />
</td>
</tr>
</table>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
I am trying to update the text and images which is already updated to the databases. I have got a admin section in which there is a news menu, where the user can edit and update the news with images. The problem is i can edit and update the news text but the images doesnt update . Below is the controller and view :
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(int id, FormCollection collection, IEnumerable<HttpPostedFileBase> files)
{
INewsRepository newsResp = new NewsRepository();
News news = newsResp.GetNews(id);
if (TryUpdateModel(news)){
newsResp.Save();
return RedirectToAction("Index");
}else{
return View();
}
}
<% using (Html.BeginForm("Edit", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<%: Html.ValidationSummary(true) %>
<table cellpadding="2" cellspacing="2" border="0">
<tr>
<td style="width:100px;">
<div class="editor-label">
Title</div>
</td>
<td>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title, new { style = "width:300px;" })%>
<%: Html.ValidationMessageFor(model => model.Title)%>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="editor-label">
Article content</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Article, new { @class = "tinymce" })%>
<%: Html.ValidationMessageFor(model => model.Article)%>
</div>
</td>
</tr>
<tr>
<td>
Image 1
</td>
<td>
<img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge %>" />
<br />
<input type="file" name="files" id="file1" />
</td>
</tr>
<tr>
<td>
Image 2
</td>
<td>
<img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge2 %>" />
<br />
<input type="file" name="files" id="file2" />
</td>
</tr>
<tr>
<td>
Image 3
</td>
<td>
<img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge3 %>" />
<br />
<input type="file" name="files" id="file3" />
</td>
</tr>
</table>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TryUpdateModel
不会更新文件。您可以手动将流复制到News
模型的相应属性中:TryUpdateModel
won't update files. You could manually copy the streams into the corresponding property of yourNews
model: