0

I am using the same model between 2 views, but when posting the model to the second view it puts all the previously entered data in the URL. Is it possible to send the populated model to the second view without posting the data in the URL?

Controller code:

    [HttpPost]
    public ActionResult ViewExample1(.Models.RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("ViewExample2", model);
        }
        return View(model);
    }

    public ActionResult ViewExample2(Models.RegisterModel model)
    {
        return View(model);
    }

Second view code where I use HiddenFor to persist the data when this view is posted back:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id="ViewExample2"})) { %>
    <%: Html.HiddenFor(model => model.UserName)%>
<% } %>
3
  • 2
    Add some code or screen example urls so people can better understand your question please. Commented Oct 24, 2012 at 7:14
  • please post here your code, it would be nice Commented Oct 24, 2012 at 7:33
  • Sorry about that, posted some example code. Commented Oct 24, 2012 at 7:57

2 Answers 2

1

When you redirect to an action with RedirectToAction(), you're doing that by GET. So the Framework passes your view model in the url to the action.

I'd suggest you to do this:

[HttpPost]
public ActionResult ViewExample1(Models.RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Do the work you want to do in the ViewExample2 action here!
        // ... and then return the ViewExample2 view
        return View("ViewExample2", model);
    }
    return View(model);
}

// This action is not needed anymore
/*public ActionResult ViewExample2(Models.RegisterModel model)
{
    return View(model);
}*/
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that is exacly what I did in the end!
0

My guess is that you're using a form tag (rather than BeginForm) and you aren't specifying a method, so it defaults to using a GET rather than a POST.

Convert to using a BeginForm, or add the method.

2 Comments

I posted the code I'm using in the second view, first view is pretty much the same. What do I need to change in BeginForm()?
You put me on the right path, thanks Mystere Man. Will post corrected code as soon as I can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.