Passing Viewbag Data from View to Controller in ASP.Net MVC3 Razor

asp.net-mvc-3 entity-framework image razor

Question

I must transmit values from the view to the controller in my ASP.Net MVC3 Razor app. One submit button is present in the display, and it is utilized to send the selected picture file as well as two other inputs. The "FileUpload" controller provided these two input values (ViewBag.Data1 = CusId; ViewBag.Data2 = Name). I must give these three (Image, CusId, and Name) to another controller when pressing the submit button in order to upload the image file.

Control Protocol

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, 0, 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();
        }

View Source

@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>
                    }
1
9
1/29/2014 9:59:00 AM

Accepted Answer

Data cannot be returned to the controller by ViewBag. These values need to be entered back into the form. The simplest option would be to put the data to the model type rather than using ViewBag.

Then, using HTML helpers like these, you may pass them with hidden inputs:

@Html.HiddenFor(item => item.CustomerId)
@Html.HiddenFor(item => item.ImageId)

You can manually add the hidden inputs if that isn't possible. The name properties are crucial for model binding, so bear that in mind.

<input type="hidden" name="CustomerId" value="@ViewBag.Data1" />
13
1/29/2014 10:12:17 AM

Popular Answer

It is true that a Viewbag cannot be passed from view to controller. But TempData can be used to pass them.

Embedding this in your view

@{TempData["Data1"]=ViewBag.Data1}
@{TempData["Data2"]=ViewBag.Data2}

However, this TempData sends the data as an object. Consequently, typecasting is required in your Controller.

int x=Convert.ToInt32(TempData["Data1"]);
string y=(TempData["Data2"]).ToString();

I tested it, and it works.

Alternately, you can provide TempData rather than Viewbag from the Controller's get method to the View's post method.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow