在我的ASP.Net MVC3 Razor项目中,我必须将值从视图传递给控制器.视图包含一个提交按钮,用于传递所选图像文件和另外两个输入数据.这两个输入数据来自名为“FileUpload”的控制器(ViewBag.Data1 = CusId; ViewBag.Data2 = Name;).提交按钮时,我必须将这三个(
Image,CusId,Name)传递给另一个控制器以上传图像文件.
控制器代码
public ActionResult FileUpload(int CusId,string Name) { ViewBag.Data1 = CusId; ViewBag.Data2 = Name; return View(); } [HttpPost] public ActionResult UploadPhoto(ElixiCustPro elixi,HttpPostedFileBase file) { //return null; try { if (file != null && file.ContentLength > 0) { if ((file.ContentType == "image/jpeg") || (file.ContentType == "image/gif") || (file.ContentType == "image/png"))//check allow jpg,gif,png { elixi.Image = new byte[file.ContentLength]; file.InputStream.Read(elixi.Image,file.ContentLength); var filename = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/ElixirFiles/UploadImagesElixir/"),filename); file.SaveAs(path); ecp.Image = new byte[file.ContentLength]; ecp.ImageUrl = path; ment.ElixiProData.Add(ecp); ment.SaveChanges(); return RedirectToAction("ImageResult"); } } } catch (Exception ex) { return View(ex.Message.ToString()); } return View(); }
查看代码
@using (Html.BeginForm("UploadPhoto","Home",FormMethod.Post,new { @enctype = "multipart/form-data" })) { @* <div class="form-group"> <label class="col-lg-2 control-label"> Customer ID</label> <div class="col-lg-10">@Html.TextBoxFor(model => model.CusId,new { @class = "form-control" })</div> <label class="col-lg-2 control-label"> Customer Name</label> <div class="col-lg-10">@Html.TextBoxFor(model => model.Name,new { @class = "form-control" })</div> </div>*@ <input type="hidden" id="id" /> <div class="col-md-6"> <div class="form-group"> <label class="col-lg-2 control-label"> DMIT Image</label> <div class="col-lg-10"> @ViewBag.Data1 @ViewBag.Data2 <input type="file" id="file" name="file"> <input type="submit" class="btn btn-success" value="Upload" /> </div> </div> </div> }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。