question

Valenciano8-8704 avatar image
0 Votes"
Valenciano8-8704 asked Valenciano8-8704 answered

Send ViewModel via RedirectToAction ?

Hi everyone,

Do you know if there is a way to send a ViewModel through a RedirectToAction method ?

Indeed I'd like to display the ViewModel errors when the ModelState is not valid inside the CreateBook method :

BookViewModel :

 using System.ComponentModel.DataAnnotations;
    
 namespace Blog.ViewModels.Home
 {
     public class BookViewModel
     {
         [Required(ErrorMessage = "Please specify a name")]
         [Display(Name = "Name : ")]
         public string Name { get; set; }
    
         [Required(ErrorMessage = "Please specify a price")]
         [Display(Name = "Price : ")]
         public int Price { get; set; }
     }
 }

Index.cshtml :

 @model Blog.ViewModels.Home.BookViewModel
 @{
     ViewData["Title"] = "Blog";
     Layout = "_Layout";
 }
    
 @section page_content{
     <p>Hello World form Blog !</p>
    
     <form novalidate asp-controller="Home" asp-action="CreateBook" method="post">
         <label asp-for="@Model.Name"></label>
         <input asp-for="@Model.Name">
         <span asp-validation-for="@Model.Name"></span>
    
         <label asp-for="@Model.Price"></label>
         <input asp-for="@Model.Price">
         <span asp-validation-for="@Model.Price"></span>
    
         <input type="submit" value="Continuer">
     </form>
 }

HomeController :

 using Blog.ViewModels.Home;
 using Microsoft.AspNetCore.Mvc;
    
 namespace Blog.Controllers
 {
     public class HomeController : Controller
     {
         public IActionResult Index()
         {
             var bookViewModel = new BookViewModel();
                
             return View(bookViewModel);
         }
    
         [HttpPost]
         [ValidateAntiForgeryToken]
         public IActionResult CreateBook(BookViewModel bookViewModel)
         {
             if(!ModelState.IsValid)
                 // When the ModelState is not valid, I'd like to redirect the user
                 // to the "Index" Action with the bookViewModel to display form errors
                 return RedirectToAction("Index", "Home", bookViewModel);
    
             // Will save the data to the DB after if ModelState is valid
    
             return RedirectToAction("Index", "Home");
         }
     }
 }

Thanks in advance for your help

dotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Valenciano8-8704 avatar image
0 Votes"
Valenciano8-8704 answered

Hi @Bruce-SqlWork , thanks for your answer.

Well I bypassed this issue by using an HttpPost version of my Index method instead, that way I can return View with the ViewModel inside.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

A redirect causes the browser to do a get. You can pass query string parameters, but there is a max length for a query string. In general is frowned upon to pass model data via a query string. Your best option is to store the model in persistent storage, and pass a key to the model data on the query string.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.