How to make redirect after validation in .net core pagemodel class

Viewed 27

I've a class like below

public class IndexModel : PageModel
{
    public PersonModel Person { get; set; }
    public ActionResult OnPostSubmit(PersonModel person)
    {
        if (person.name !=null)
        {
            ViewData["Name"] = person.Name;
            return Redirect("~/path/");
        }
    }
}

here I'm getting not all code paths returns a value error. I just want to
redirect to another page if name property has value else i want stay in the same page.let me know if you have a solution. thanks

1 Answers

You can try to use TempData to replace ViewData,ViewData cannot be passed with redirect in razor page:

public ActionResult OnPostSubmit(PersonModel person)
        {
            if (person.Name != null&&person.Name!="")
            {
                TempData["Name"] = person.Name;
                return Redirect("~/path/");
            }
            return Page();
        }
Related