ModelState validation on RedirectToAction in MVC

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



ModelState validation on RedirectToAction in MVC



I want to implement ModelState.AddModelError on RedirectToAction in MVC. Currently, I am not getting error message. Can anyone please help me?


ModelState.AddModelError


RedirectToAction


if(ModelState.IsValid)

var HList = hDetails.HTrackerList().Where(x => x.AccId == user.AccountID && x.UserId == user.Id).Select(y=>y.HorseId).ToList();
var datainList = HList.Contains(model.HorseId);

if(!datainList)

hDetails.InsertHorse(model);

else

ModelState.AddModelError("datainList", "Horse is already exist. Do you want to update it?");



return RedirectToAction("Home");



In the View:


@Html.ValidationMessage("datainList")





return View, do not redirect, if you want to see error message. return View();
– Amit Kumar
Aug 9 at 11:55


View


return View();





You are redirecting - ModelState is only applicable in the current method when you return the view. One option would be to send that error message as query string value, or alternatively add it to TempData (and then get it again in the GET method and add it to ModelState)
– Stephen Muecke
Aug 9 at 11:56



ModelState


TempData


ModelState





@StephenMuecke, can you please show me how to send it through query string
– MVC
Aug 9 at 12:03





return RedirectToAction("Home", new { error = "Horse is already exist .... "); and then add a string error parameter in that method. But in the get method, I would just display that in the view as some text, because its not really a ModelState (validation) error
– Stephen Muecke
Aug 9 at 12:08


return RedirectToAction("Home", new { error = "Horse is already exist .... ");


string error


ModelState





@StephenMuecke, you are right. This is not actually modelstate error. Sorry to ask you, please show me how to do this in view part as well.
– MVC
Aug 9 at 12:10




1 Answer
1



In your ontroller class:


public class YourController : Controller

[HttpPost]
public ActionResult TestMethod()

if(ModelState.IsValid)

var HList = hDetails.HTrackerList().Where(x => x.AccId == user.AccountID && x.UserId == user.Id).Select(y=>y.HorseId).ToList();
var datainList = HList.Contains(model.HorseId);

if(!datainList)

hDetails.InsertHorse(model);

else

TempData["datainListError"] = "Horse is already exist. Do you want to update it?";



return RedirectToAction("Home");




Then in the Home Controller:


public class HomeController : Controller

public ActionResult Index()

if(TempData.ContainsKey("datainListError"))

ViewBag.ErrorMessage = TempData["datainListError"].ToString();


return View();






Then in the Index View:


@if(ViewBag.ErrorMessage != null)

<div class="alert alert-danger">@ViewBag.ErrorMessage</div>





Thank you. solved my problem.
– MVC
Aug 9 at 20:27





Great to hear that!
– TanvirArjel
Aug 9 at 20:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS