0

MVC newbee here. I have a MVC3 project with a main View (Index.cshtml). From this view you can go to the CreateRemark.cshtml view. You can create and save a remark on this view. In the POST action I have logic to persist the data to the database and then I want to render the Index view again.

I do this at the end of my action CreateRemark by writing:

Return View("Index");

I expected that my URL would look like localhost/ or localhost/Index. But it keeps saying localhost/CreateRemark?id=123, while it does actually render the html which is on the Index view. It renders it wrong though (I haven't investigated fully but it's missing all the images in the buttons and the hierarchical grid misses the 2nd level) and I think I can solve this by making sure I direct the enduser to the Index View in the correct MVC manner. I think I'm doing it wrong.

The GET/POST actions Index and CreateRemark are both in the same Home controller by the way. I think I want a RemarkController later, but haven't done that yet. Sorry.

Can someone point me in the right direction please? Thanks in advance for your help.

1
  • Views do not correspond to URLs, your route configuration does (which by default uses {controller}/{action}. Commented Mar 11, 2013 at 13:28

2 Answers 2

3

You are simply returning a different view from the same controller action so the URL will remain the same.

If you simply want to have the same action happen as if the user then called the index action use

  return RedirectToAction("Index");

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction%28v=vs.108%29.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

The url is whatever the user navigated to. If they navigated to that page (I assume in the "From this view you can go to the ..." bit), then that is the url.

The string you supply in Return View("Index") has got nothing whatsoever to do with URLs; that just tells the view-engine which view file to use. The only way you can change the URL at that point would be to return a redirect to the index action / route:

return RedirectToAction("Index");

That would involve an extra http request for the client, note.

2 Comments

Thank you for the additional information. Is an extra http request for the client bad? It will slow things down I assume? The problem is, if I dont give this redirect the hierarchical grid I use is rendered wrong for some reason.
"bad" is a little subjective. Less direct, certainly. At a minimum it will add whatever the latency is

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.