locked
Accessing data between multiple forms in one view with multiple actions MVC Razor C# RRS feed

  • Question

  • Hi,

    I'm trying to access data between two forms in one view. I'm pretty new to the C# MVC programming, please guide me on below blocks,

    I have following forms defined in view,

    @model Mysite.Models.FileUpload
    @using Mysite.Models
    @{    
        var FileUploadData = ViewData["FileUploadData"] as FileUpload;    
    }
    @using (Html.BeginForm("LoadData",
                            "LoadAndAnalyzeData",
                            FormMethod.Post,
                            new { enctype = "multipart/form-data" }))
    {
        <div class="div-root" id="serverslist">
            @Html.LabelFor(m => m.SelectedServer)
            @Html.DropDownListFor(m =>m.SelectedServer, FileUploadData.ServersList)
        </div>
        <div class="div-root">
            <label for="files">Choose File 1: </label>
            <input type="file" name="file1" id="file1" />
        </div>
           <div class="div-root">
             <input type="submit" id="upload" name ="FileUpload" value="Upload Files For Analysis" />         
        </div>
    }
    @using (Html.BeginForm("AnalyzeLoadedData", "LoadAndAnalyzeData"))
    {
        <input type="hidden" name="servername" id="servername" value='@ViewBag.servername' />
        @ViewBag.servername
        <input type="submit" id="analysis" name ="AnalyzeLoadedData" value="Analyze Loaded Data" />
    }

    Issue:
    I'm trying to get the servername submitted by form1 to form2 as a hidden value. I'm doing this through ViewBag where i'm setting servername in Action1(LoadData) & setting this value in form2. I can see the value displayed on the page, But when i submit Action2(AnalyzeLoadedData) is not getting the servername from form2. I'm seeing following message,
    "servername The name 'servername' does not exist in the current context"

    Please someone guide me in solving this.
    Thank you.

    • Moved by CoolDadTx Wednesday, March 8, 2017 3:45 PM ASP.NET related
    Saturday, March 4, 2017 10:49 PM

Answers

  • Hi dvsrk,

    I agree with Ouch Liu.

    I also made a small demo, please have a look.

    we can use TempData in a practical scenario to pass data from one controller action to another

    //Controller Action 1 (TemporaryEmployee)
     public ActionResult TemporaryEmployee()
    {
                    Employee employee = new Employee
                    {
                            EmpID = "121",
                            EmpFirstName = "Imran",
                            EmpLastName = "Ghani"
                    };
                    TempData["Employee"] = employee;
                    return RedirectToAction("PermanentEmployee");
    }
    
     //Controller Action 2(PermanentEmployee)
     public ActionResult PermanentEmployee()
    {
                   Employee employee = TempData["Employee"] as Employee;
                   return View(employee);
     }
    

    It’s best fit for scenarios when:

    • we need to preserve data from current to subsequent request.
    • passing error message to an error page.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by dvsrk Saturday, March 18, 2017 6:39 PM
    Wednesday, March 8, 2017 2:29 AM

All replies

  • Hi,

    I'm wondering if you're trying to pass data cross actions.

    If you do, I would like to suggest you using TempData instead of ViewBag or ViewData.

    I think this post should be helpful to you:

    Passing Data Using ViewData, ViewBag, TempData, Session Variables In ASP.NET MVC


    Ouch Liu , Developer ≈ Designer

    Sunday, March 5, 2017 5:01 PM
  • Hi dvsrk,

    I agree with Ouch Liu.

    I also made a small demo, please have a look.

    we can use TempData in a practical scenario to pass data from one controller action to another

    //Controller Action 1 (TemporaryEmployee)
     public ActionResult TemporaryEmployee()
    {
                    Employee employee = new Employee
                    {
                            EmpID = "121",
                            EmpFirstName = "Imran",
                            EmpLastName = "Ghani"
                    };
                    TempData["Employee"] = employee;
                    return RedirectToAction("PermanentEmployee");
    }
    
     //Controller Action 2(PermanentEmployee)
     public ActionResult PermanentEmployee()
    {
                   Employee employee = TempData["Employee"] as Employee;
                   return View(employee);
     }
    

    It’s best fit for scenarios when:

    • we need to preserve data from current to subsequent request.
    • passing error message to an error page.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by dvsrk Saturday, March 18, 2017 6:39 PM
    Wednesday, March 8, 2017 2:29 AM
  • Please post questions related to MVC in the ASP.NET forums.
    Wednesday, March 8, 2017 3:45 PM
  • sure, thank you.
    Saturday, March 18, 2017 6:39 PM