I have a razor view with the path ~/Views/ReservationDetails/AddMeals.cshtml which is rendered by the controller ReservationDetails. In the View I use the following Html helper action link:
#Html.ActionLink("Wróć", (ViewBag.ReservationId!=0?"Edit":"Create"),"ClientReservations",new { id=ViewBag.ReservationId})
When I edit MealsReservations and I want to come back to Edit it redirects me to ReservationDetails/Edit/Length=18?
However when I add meals for reservation for the first time and I want to come back, then it redirects me properly to Create in controller ClientReservations.
What can it be the reason?
Related
I have page that has 2 forms posting back to the same controller
The first form posts back and the result sends back an updated model to allow the second form to appear on the page.
Concept is the user is shown items from a list they want to select to return.
HTML for this is as follows:
The page sends that form back to this controller
With this model
The 'GetModel' is the model that is past back to the page
Then sends back to the same page to allow a preview of what they want to return for the user to confirm.
When the second form as below:
is posted back the the same controller and function again model is completely empty but cant see why?
I have a index page for which the the layout page is different and there is another index page in a different folder under the same project. Both these index pages have one and two partial views in each and also have their own controllers, one is home and the other testController.
My problem is when I hit f5 in the browser, it is calling the partial view method and index method for both the controllers home and the testcontroller and it is clearing off certain variables. My understanding is that it must call only the index method of the testcontroller and call the partial views which is in this page and controller. Is my understanding wrong?
In other words, you have a view that calls child actions. When you use Html.Action or Html.RenderAction, for the most part, it's as if you went to those actions directly via a URL. Everything that needs to happen to route to an action, run it, and return a rendered view has to happen for a child action, just as with a normal action.
Where your question gets confusing is in what you're expecting to happen when you refresh the page. Refreshing the page will replay the request to the server, which must then create and send the response just as it did for the original request. That means, your main action and any child actions will be hit again. I'm not sure why you would think something else should happen. Perhaps, you're wanting to only refresh a portion of the page? That's where AJAX comes in, but it won't help you with something like pressing F5. That will always cause the full page to be reloaded.
I figured out the problem I had with both controller index methods being called. I was using the same layout page in both index pages. Point one of the index pages to the right layout page stopped calling the Home controllers index method.
I have some action's views that are dependant on settings from DB (for example display textbox or not). Settings are changed in admin controller.
What I'd like to achieve is to have the preview of the changed views in admin's sidebox (right side of the screen) after the changes will be saved to DB.
Is there a way to get View result of another action (casually returns View()) with button (Save and Preview) click in form of string (HTML) to display in the sidebox?
Or maybe has anyone other and better idea?
Yes. They're called child actions. Simply, you just call the action you want rendered via Html.Action:
#Html.Action("SomeAction", "SomeController")
However, there's a couple of things to keep in mind. First, your child action should return PartialView. Otherwise, you'll get the full layout rendered again where you call the action. If you want to use the same action for both a regular view and as a child action. You can branch your return:
if (ControllerContext.IsChildAction)
{
return PartialView();
}
return View();
Second, if you only return PartialView, then the action should not be available to directly route to. Otherwise, someone could enter a URL in their browser to go to this child action and only the partial view would be returned, devoid of layout. You can prevent this from occurring using the ChildActionOnly attribute:
[ChildActionOnly]
public ActionResult MyAwesomeChildAction()
{
...
}
Then, this action will only be available to be called via Html.Action.
'm using ASP.NET MVC 3 with Razor views. I have a partial view which I would like to render on all pages so I am placing it in the site's main Layout page. However, I am not sure the best way to load data into the partial view. I could load it for each ActionMethod but there is there a way to do this globally across the entire app?
Move all the data loading logic for your partial into a separate action method.
Then in your layout page, instead of rendering the partial with a call to RenderPartial(), call the RenderAction() method.
RenderAction() makes a "child" action call - thus putting all the logic needed for that partial into one place.
Write action for this partial view in MasterController because every controller inherits from it, and place your partial view in shared folder and call it on Site's master page (like every site has a user control which gives login box until user is logged in else it displays logged in user info) ... hope it answer your ques ...
I have a requirement to include logon fields in my right column of a 3 column layout, with the body view being the centre. Right now I simply have Logon actions in my home controller, but IN haven't given this much work yet and am running into difficulties. I use a partial view for the HTML form with login fields, but I want to move the logon actions into my Account controller. However, my quandary is how to. so to say, return a partial view from an action method, e.g. for a bad logon message.
For that purpose, Html.RenderAction is used. Simple way to inject output of other menu, logon, etc. actions into generated html
Do you use AJAX or you submit the form back?
I would suggest using AJAX to send the form data. If the login is unsuccessful you can return some string or whatever message you like (you can also return the partial view), if it is successful you can authenticate the user and redirect to home page.