image – 将Viewbag数据从View传递到ASP.Net MVC3 Razor中的Cont
发布时间:2020-12-16 07:36:23 所属栏目:asp.Net 来源:网络整理
导读:在我的ASP.Net MVC3 Razor项目中,我必须将值从视图传递给控制器??.视图包含一个提交按钮,用于传递所选图像文件和另外两个输入数据.这两个输入数据来自名为“FileUpload”的控制器(ViewBag.Data1 = CusId; ViewBag.Data2 = Name;).提交按钮时,我必须将这三个(
在我的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> } 解决方法
ViewBag无法将数据传回控制器.您应该将这些值发布回表单中.最简单的方法是不使用ViewBag并将数据添加到模型类型.
然后,您可以使用HTML帮助程序传递隐藏输入,如下所示: @Html.HiddenFor(item => item.CustomerId) @Html.HiddenFor(item => item.ImageId) 如果不可能,您可以手动添加隐藏的输入.请记住,名称属性对于模型绑定很重要. <input type="hidden" name="CustomerId" value="@ViewBag.Data1" /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 表单认证ReturnUrl和子域名用于单点登录
- asp.net-mvc – 客户端验证未显示消息
- asp.net – 如何在WebGrid中的列标题使用DisplayName数据注
- asp.net-mvc – MVC3非顺序隐藏输入索引需要先来吗?
- iis-7 – ASP经典IIS7问题
- 在ASP.NET中的OpenID认证?
- asp.net-mvc – 在html5输入模式属性中使用“@”和MVC
- 代理模式-aop
- asp.net – 有没有办法在Web Api控制器中处理表单发布数据?
- asp.net-mvc – CultureInfo:ASP.NET / MVC上的Haitian Cr