Home Can I append data from controller to url and still render same page?
Reply: 3

Can I append data from controller to url and still render same page?

Activex Published in 2018-01-11 17:09:47Z

I have a simple MVC application that presents an IQ test to the person.

I have a controller call TestMyIQController and on this controller I have three action methods and corresponding to three views.

1) Index.cshtml

2) Questions.cshtml

3) Score.cshtml

On the first page localhost:12345/TestMyIQ/Index of the application I ask the user to input their First and Last name and click button Next to go to second page.

On the second page of the application localhost:12345/TestMyIQ/Questions I present a few multiple choice questions and I have the user select True/False for each question. Once the user complete all the question she can click button Submit to see her IQ score.

On the final score page localhost:12345/TestMyIQ/Score I show the score via a ViewBag object.

However, I want to know how to modified the final page url so that I can append the First Name of the user? For example if the person name is John Doe then I want my final score url will be like this localhost:12345/TestMyIQ/Score?firstname=John.

Note it is not necessary to append the First Name to all other url...because I only want it on the final score page.

Please help me. Thanks

Niko Reply to 2018-01-11 18:25:49Z

Not sure I understand your needs, but you dont have to store the name in url, you can use for example session variable, but if you insist on it being on url, why not just redirect(urlWithParameter).

For a good listing of possibilities, see Msprogrammer

Andre Reply to 2018-01-11 18:46:53Z

Considering you received the first name in the Questions action, and added as a property of your model, you can add it to your form as a hidden field:


    @Html.HiddenFor(x => x.UserFirstName);
    // rest of form


public ActionResult Question(QuestionModel model)
    // form processing
    return RedirectToAction("Score", new{ firstName = model.UserFirstName })

Because in the Question page you stored it as a hidden field, it will only show up in the url of the score page.

But keep in mind that while this is ok if you're learning and just want to do some cool stuff to show your friends, this isn't the best way to handle all of this.

RajN Reply to 2018-01-12 00:41:54Z

Session may be choice to store the name and retrieve it on the score page.

public ActionResult Index(UserModel model)
    // save the name 
    Session["FirstName"] = model.FirstName;

    return RedirectToAction("Questions")

On the score method, retrieve the name from session

public ActionResult Score(ScoreModel model)
    // save the name 
    string firstName = (string)Session["FirstName"];

    return RedirectToAction("Index")

Hope it helps.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321696 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO