加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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" />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读