What is the best method to pass data from one view to another Asp.net MVC

Sammani Palansuriya 6 Reputation points
2021-08-25T10:48:53.837+00:00

Hi,

I'm having a Asp.net MVC application.

  1. We have a form which we enter data
  2. Once clicked save we route user to confirmation page with some values set in the form
  3. If user clicks ok only it will save to Database
  4. If user clicks on back button on confirmation page we need to show the changes made in the form

Issue we have is when we pass the model from save form to confirmation page, we show only some values in the page. and these are also set on HTML.Display for. So when back button is clicked data is not post back.

What is the best way to achieve

  1. Set all the model data in hidden fields in the confirmation page and post back
  2. Set model in view bag
  3. Load confirmation page as a partial view

or any other?

Would like to get opinions on this.

Regards,

Sammani

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,138 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,866 Reputation points
    2021-08-25T14:40:15.71+00:00

    The view bag exists only for one request, so can not be used. I prefer storing the data on the server between pages, but if the data is not too large, you can use posts.

    The other option is a SPA type application where the data is all in the client, and Ajax calls are used instead of form posts.

    0 comments No comments

  2. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2021-08-29T07:59:04.953+00:00

    Hi @Sammani Palansuriya ,

    If user clicks ok only it will save to Database

    If user clicks on back button on confirmation page we need to show the changes made in the form

    Issue we have is when we pass the model from save form to confirmation page, we show only some values in the page. and these are also set on HTML.Display for. So when back button is clicked data is not post back.

    Based on your description, in the confirmation page, you could display the properties via the input text tag (set it as read only) and use tag helper to bind the value, then you can add two submit buttons to submit the form to different page.

    Please refer the following sample:

    The Controller has the following actions:

        [HttpGet]  
        public IActionResult Create(Student student)  
        {  
            if (student == null)  
                student = new Student();  
            return View();  
        }  
        //  
        [HttpPost]  
        public IActionResult Confirmation(Student student)  
        {  
            return View(student);  
        }  
    
        //if user click the Save button in the Confirmantion Page, submit the form to this action to save data into database.   
        [HttpPost]  
        public IActionResult Save(Student student)  
        {  
            if (ModelState.IsValid)  
            {  
                //dosomething: store data into database.  
            }  
            return View(nameof(Index));  
        }  
    

    Create view page: After user click the Submit button, the form data will submit to the Confirmation action method.

    @model Core3_1MVC.Models.Student  
      
    @{  
        ViewData["Title"] = "Create";  
    }  
      
    <h1>Create</h1>  
      
    <h4>Student</h4>  
    <hr />  
    <div class="row">  
        <div class="col-md-4">  
            <form asp-action="Confirmation">  
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
          
                <div class="form-group">  
                    <label asp-for="LastName" class="control-label"></label>  
                    <input asp-for="LastName" class="form-control" />  
                    <span asp-validation-for="LastName" class="text-danger"></span>  
                </div>  
                <div class="form-group">  
                    <label asp-for="FirstMidName" class="control-label"></label>  
                    <input asp-for="FirstMidName" class="form-control" />  
                    <span asp-validation-for="FirstMidName" class="text-danger"></span>  
                </div>  
                <div class="form-group">  
                    <label asp-for="EnrollmentDate" class="control-label"></label>  
                    <input asp-for="EnrollmentDate" class="form-control" />  
                    <span asp-validation-for="EnrollmentDate" class="text-danger"></span>  
                </div>  
                <div class="form-group">  
                    <input type="submit" value="Create" class="btn btn-primary" />  
                </div>  
            </form>  
        </div>  
    </div>  
    

    Confirmation view page: there have two submit button, and we use the formaction attribute to submit the form to different action, and use the formmethod attribute to set the submit method.

    @model Core3_1MVC.Models.Student  
      
    @{  
        ViewData["Title"] = "Confirmation";  
    }  
      
    <h1>Confirmation</h1>  
      
    <div>  
        <h4>Are you sure you want to insert the following Student? </h4>  
        <hr />  
        <form action="">  
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
              
            <div class="form-group">  
                <label asp-for="LastName" class="control-label"></label>  
                <input asp-for="LastName" class="form-control" readonly />   
            </div>  
            <div class="form-group">  
                <label asp-for="FirstMidName" class="control-label"></label>  
                <input asp-for="FirstMidName" class="form-control" readonly />   
            </div>  
            <div class="form-group">  
                <label asp-for="EnrollmentDate" class="control-label"></label>  
                <input asp-for="EnrollmentDate" class="form-control" readonly />   
            </div>  
            <div class="form-group">  
                <input type="submit" value="Save" class="btn btn-primary" formaction="Save" formmethod="post" />   
                <input type="submit" value="Cancel" class="btn btn-primary" formaction="Create" formmethod="get" />  
            </div>  
        </form>  
    </div>   
    

    The result as below:

    127353-30.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments