I am posting a form to url /Admin/EditInPlace from page /About
In EditInPlace's conroller code i can find the Url-Referrer i.e. from which page request is coming. But how can i map the Url-Referrer to its View page??
In short how can i found that a page /xyz is mapping to which View.aspx file programmatically?
Needing to know what View the user was previously looking at smells like a design problem. As a workaround, consider passing a query string parameter to the Admin controller's EditInPlace method that describes which UI the user just saw.
/Admin/EditInPlace?channel=basic
Related
I'm working on this project that currently has the follow method:
[HttpPost]
public ActionResult Service(string identifier)
This function is currently being used by a webpage form
<form method="POST" action="/Connector/Service">
<input type="hidden" id="identifier" name="identifier" value="#TempData["Identifier"]"/>
So Service is used when the user clicks on a button that submits the form on the webpage. The user is taken to this page from another action.
I have to implement a feature where sometimes instead of taking the user to the webpage, the user goes directly to the Service method from the action.
I found this thread when searching: ASP.NET MVC: RedirectToAction with parameters to POST Action
But it seems like that is bad design and returning RedirectToAction with Service actually did not work for me.
return RedirectToAction("Service", new {identifier})
Upon more search it seems like I actually cannot make a post request from my controller. Asp.NET MVC : redirect to another controller with POST Action
Any ideas on what I could do here? I am fairly new to ASP.NET and have no idea what to do at this point. All help is appreciated, thanks.
So turns out I could just call the function directly by doing
Service(identifier)
I kept thinking I had to use RedirectToAction so that didn't even cross my mind.
Thanks Sherlock for the answer!
The tag [post] is used only when a form is submited by the view itself, if your page redirect to a controler function from another view than the one your in, you have two option:
Use the [get] tag, you will probably have more luck with this tag, although i don't know exactly how it work (this might be edited)
Use the tagless function with parameters, if you rediret from another view to a function with a parameter, like public ActionResult Service(string identifier) that has no tag, you will automatically reach this function.
if the parameter haven't been specified, it will simply be NULL.
Quite new to MVC so please bear with me. I'm trying to load a partial view in a modal from a different controller and everytime I try to view it I get just an empty modal. I believe it is because I haven't been able to instantiate my model in the view.
E.g. I have a Controller 'Home' with a method 'Details' that returns a partialview from a different View folder. The native model to the controller is 'model', whereas the model belonging to my other controller is 'model2'.
public ActionResult Details() {
model2.User = user; //this is a global variable
model2.GetDetails();
return PartialView("~/Views/...Details", model2);
}
I'm sure the reason is because i'm missing the model data in the view. I tried adding another #model... to the view but clearly this doesn't work.
Is there a way of doing what I am trying to accomplish? It can even be a relatively dirty solution as this is a stopgap solution for the time being.
Reading back over this post it reads a little convoluted so if any clarification is needed please let me know.
Thanks
I faced this problem once before and i think it's a lot of work to reproduce it to provide an exact solution, but I can offer my 2 cents. The thing with browser Modals is that you need to provide a url when you are opening it. The URL will have to be the Controller/Action url and this is the tricky part which causes the problem. If you can figure that out, you should be able to solve the problem. If you can't, you can do one of the following:
1. Set the HTML content of Modal dialog from your main window's JS code after the Modal is opened.
2. Use one of the 3rd party HTML/CSS modal implementation, and set the HTML content from the JS code. In this case there is no browser modal and everything is on the same page.
To verify if the view is returning correctly from XHR, put the actionRoute URL in the browser address bar and you will see the content getting returned. It will help with troubleshooting.
Imagine the ForgotPassword sent an email with a link to Recover the password. Ideally we want the RecoverPassword to be a PartialView and it has to run inside the HomePage itself.
The external link passes a GUID.
QUESTIONS:
1) What's the right way to tell the Home Page to display the Partial View only on this specific case?
2) What would be the URL link look like?
3) What would the HomePage Index Controller look like so that it would also handle the possibility of a ResetPassword external link request?
You could do something simple like have a query string. for example www.yoururl.com/index?showresetpassword=true
Then inside your view you can add an if statement to render the partial view or not. If you need it inside of the controller I suggest you have the parameter as a nullable bool. Something like
public ActionResult Index(bool? showresetpassword)
Make it nullable so that if it is not inside of the url you will not have any isssues.
I'm developing a new website for my employer which contains a lot of CMS-style features. One of them is being able to create pages, specify URLs, menus, etc.
This is all fine.
What I'm after is a way to create a "Preview" button on these pages, that does not write to the database. I want to post (preferably the entire Model, if not, just the form data) to a new window and have that view render the page.
I have searched high and low and I cannot find an example that makes sense. Most people seem to have given up :/
I've attempted this by myself with TempData, however TempData gets cleared before I can render the page (as there is more than 2 steps involved from what I can deduce..).
I must POST as the form data will contain HTML (inside a Telerik Editor control).
Does anyone have any idea on a nice way to accomplish this? Or can anyone provide links to some resources? (I've come up completely blank!)
Regards,
chem
You can store the model in Session State.
I would store the data in session state.
this may be useful:
http://davidhayden.com/blog/dave/archive/2011/02/09/SessionLessControllersMvc3.aspx
You can still use TempData. At first step you need explicitly tell leave value in Session, for example:
string messageValue = (string) TempData.Peek("message"); // Does not cause ejection
You could serialize the model to XML and then the action method for the preview page could deserialize it.
See this link.
Thanks for the help guys/gals.
I managed to use TempData.
I basically ajax post to an action that stores the content in TempData using a Guid as the key. The Guid is returned and once the ajax post returns a hidden form with a target="_blank" action posts to a Preview action method passing through the Guid. That preview action then renders the view with the content in TempData.
Thanks for the suggestions.. it got me thinking!
Regards,
chem
I have a contoller action that a number of forms will post to, all in different views.
Is there a way, in my controller action, to see which view contained the form that posted to it?
I need this to determine to where to redirect the action when the code in the post action is complete.
Thank you!
Two options...you can add a field to your form (or query string) that provides the redirect url. Or you can look at the HttpRequest.UrlReferrer field. It will provide you with the full URL which you have to parse to get the original form.
Hope this helps