I'm wondering if there is an ASP.Net MVC equivalent to PHP's require_once() function.
Lets say I need to call RenderAction to a particular action twice. However, inside the view that is rendered from said action, I need to print out some init scripts but only once.
Is there an easy mechanism to do this?
Should a flag be set in the controller and then passed into the view?
Thanks for any help you can provide.
Edit
Thanks for the responses. I know I can do this through the controller or temp data or some mechanism like that but wasn't sure if there was anything built into .NET MVC that would have done this for me automatically.
I am not aware of such equivalent.
Should a flag be set in the controller and then passed into the view?
That seems like a good way. The flag needs to be passed as argument to the controller action when using the RenderAction helper.
Related
I'm trying to use T4MVC in my ASP.NET MVC4 application.
I'm inserting it in my view as shown below:
#using (Ajax.BeginForm(MVC.Check.Lead(),
The Lead method takes an integer ID parameter which is supplied in the form itself.
In the documentation T4MVC state that a parameter-less constructor should be auto generated however the compiler says that there isn't.
Why is it not generated?
Well I figured it out myself. My controller action returned a string which apparently is not suppported by T4MVC. So I changed it to partialviewresult and it works now.
Another possible reason (for future viewers) is that your action is inherited. It works but T4MVC will not automatically make your base actions virtual and it will not generate the parameter-less overload.
I need to get some Json to the client side from the server and it's somewhat troublesome as almost all tutorials assume an Ajax call to a separate action to get the Json.
This led me to think that there must be some reason why it is done this way. In Asp.Net MVC we can pass a Model along with the view to get the information but we can't seem to easily pass a Json object. Instead you are supposed to make a separate call to get this information.
What if the Json info is known when the page is generated, why not generate it at the same time?
I'm sorry if I wasn't clear enough. While it's nice to hear of ways to get Json to the client, the question is actually whether there is a specific reason the Ajax call method is much more popular, like security or anything like that.
Can you put something like this into your view? (rough pseudo code, assuming using a Razor view)
< script >
var myJSON = { Field: #model.Field, Field2: #model.Field2 };
< /script >
Because you do not need both at the same time... on the first call will be to get html (the view of the data - represented by a view model), and any ajax calls will be to get the possibly updated data (json serialized view model).
No reason why you can't. You could use the javacript serializer to create a JSON string that drop on the page. You could also create an action that return the json string that you called from a script tag.
What you want if you're using KnockOut, would be the Mapping plugin that turns an ordinary JS object, like that generated above, into an observable ready for KnockOut to use. See here from info. http://knockoutjs.com/documentation/plugins-mapping.html
You can use content-negotiation by setting accept header. This is considered a best practice (and according to some RESTful).
This needs to be supported and implemented at server as well. ASP NET MVC does not make it easy to support content-negotiation and you have to implement it yourself by if-else or using ActionFilter and implementing action selector.
I've been using Html.Action("ActionName", "ControllerName") to invoke child actions across controllers without needing to have the view in Views\Shared. This has been working great for displaying things like session or cookie information.
Instead of just accessing cookies, I would like to pass additional parameters to Html.Action("ActionName", "ControllerName") so the action can execute different code based on the the data passed to the original view.
Should I be using a different method to pass parameters to a child action in a different controller? How would one accomplish this?
You could specify additional data in the RouteValues property like this.
Html.Action("ActionName","Controller", new { id = 1 })
To add a little on this question, I am using ASP.Net MVC 5 and I could succeed to achieve this with this code:
#Html.Action("foo",new {parameter1=1})
I have an action-method in a controller that takes requests coming from a variety of different views.
It is somewhat of a utility method and I simply want it to accept the parameters it is given - do something - and then refresh the view that sent the request.
Right now, the only way I see to do this is by having the method figure out what view sent it the info and do a:
return RedirectToAction("method", "controller");
For each possibility (or something similar to that).
Is there a more general way I can make my method just re-render the current view without having to explicitly identify it?
-Thanks
Your best bet is to use jQuery to post the data then utilize the results as you see fit. Otherwise you can pass in the action/controller name in the post and use them dynamically to redirect.
I call the above line in my asp.net mvc view. How do i retrieve the dictionary values from the test controller?
You can pass information using ViewData e.g. ViewData["MyDictionary"] = myDictionary;
You can't. The point of RenderAction is to let the controller you're calling do what it normally would without you worrying about what's happening. Maybe a partial view might be better suited for you in your case, it's hard to say without seeing the big picture...