大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – MVC:如何将文件上传和其他表单字段发布到一个操作,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Html.beginForm("Create","Document",FormMethod.Post,enctype = "multipart/form-data")
和
Html.beginForm()
我的看法如下:
<%@ Page title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Publications.WebUI.Models.DocumentEditViewModel >" %> <asp:Content ID="Content1" ContentPlaceHolderID="titleContent" runat="server"> Edit </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <fieldset> <legend>Edit <%= Html.Truncate(Model.Document.title,50)%></legend> <%= Html.ValidationSumMary(false) %> <% using (Html.beginForm()) { %> <div class="editor-label"> <%= Html.LabelFor(model => model.Document.titlE) %> </div> <div class="editor-field"> <%= Html.HiddenFor(model => model.Document.DocumentId ) %> <%= Html.ValidationmessageFor(model => model.Document.titlE) %> <%= Html.TextBoxFor(model => model.Document.titlE)%> </div> <div class="editor-label"> <%= Html.LabelFor(model => model.Document.DocumentUrl)%> </div> <div class="editor-field"> <%= Html.ValidationmessageFor(model => model.Document.DocumentUrl)%> <%= Html.TextBoxFor(model => model.Document.DocumentUrl)%> </div> <div class="editor-label"> <%= Html.LabelFor(model => model.Document.Description)%> </div> <div class="editor-field"> <%= Html.ValidationmessageFor(model => model.Document.Description)%> <%= Html.TextAreaFor(model => model.Document.Description) %> </div> <div class="editor-label"> <%= Html.LabelFor(model => model.Document.ThumbnailUrl )%> </div> <div class="editor-field"> <% using (Html.beginForm("Create",new { enctype = "multipart/form-data" })) {%> <%= Html.ValidationmessageFor(model => model.Document.ThumbnailUrl )%> <input name="uploadFile" type="file" /> <% } %> </div> <div class="formActions"> <div class="BACkNav"> <%= Html.ActionLink("< BACk to List","Index") %> </div> <div class="submit"> <input type="submit" value="Save" /> </div> <% } %> </div> </fieldset> </asp:Content>
我的控制器只需要使用Document模型和httpPostedFileBase,并尝试将文件上传到服务器并将文档保存到存储库
[httpPost] public ActionResult Create(Document document,httpPostedFileBase uploadFilE) { if (ModelState.IsValid) { //Process file upload //update repository } return View("List"); }
所以我想知道是否可以做同样的操作的文件上传和更新存储库,我应该如何构造我的视图来促进这一点。
视图
<asp:Content ContentPlaceHolderID="MainContent" runat="server"> <h1>Edit <%= Model.Name %></h1> <% using (Html.beginForm("Edit","Admin",new { enctype = "multipart/form-data" })) { %> <%= Html.Hidden("ProductID") %> <p> Name: <%= Html.TextBox("Name") %> <div><%= Html.Validationmessage("Name") %></div> </p> <p> Description: <%= Html.TextArea("Description",null,4,20,null) %> <div><%= Html.Validationmessage("Description") %></div> </p> <p> Price: <%= Html.TextBox("Price") %> <div><%= Html.Validationmessage("Price") %></div> </p> <p> Category: <%= Html.TextBox("Category") %> <div><%= Html.Validationmessage("Category") %></div> </p> <p> Image: <% if(Model.ImageData == null) { %> None <% } else { %> <img src="<%= Url.Action("GetImage","Products",new { Model.ProductID }) %>" /> <% } %> <div>Upload new image: <input type="file" name="Image" /></div> </p> <input type="submit" value="Save" /> <%=Html.ActionLink("Cancel and return to List","Index") %> <% } %> </asp:Content>
控制器
[AcceptVerbs(httpVerbs.Post)] public ActionResult Edit(Product product,httpPostedFileBase imagE) { if (ModelState.IsValid) { if (image != null) { product.ImageMimeType = image.ContentType; product.ImageData = new byte[image.ContentLength]; image.InputStream.Read(product.ImageData,image.ContentLength); } productsRepository.SaveProduct(product); TemPDAta["message"] = product.Name + " has been saved."; return RedirectToAction("Index"); } else // Validation error,so redisplay same view return View(product); }
以上是大佬教程为你收集整理的asp.net-mvc – MVC:如何将文件上传和其他表单字段发布到一个操作全部内容,希望文章能够帮你解决asp.net-mvc – MVC:如何将文件上传和其他表单字段发布到一个操作所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。