So I have a scenario where I want to return my ActionResult...Return View("ViewName", "MasterPageName",model); in a popup window of a specific size I can pass...
E.G.:
public ActionResult PopUp()
{
//do some work...
//I want this returned in a popup window/modal dialog of some sort...
return View("ViewName","MasterPageName",model);
}
what is a reasonable way to accomplish this from the controller in asp.net mvc?
thanks in advance.
Nothing can be done on server side but you can decorate your action links like
<%= Html.ActionLink("Pop Up", "PopUp", null, new {target="_blank"}) %>
You can't manipulate the client side browser from a Controller on the server-side. What you can do is output script in your returned view, or call a controller that returns data via an AJAX call and pop-up from the client-side.
This is not something you can really do from your controller as this is code that executes on the server as the result of a http request and returns a response of some form or other. You will need to do this on the client, probably using javascript or alternatively you can call your controller action and specify the target attribute of the a tag as '_blank'.
Maybe you can try dynamically loading the rendered view using jQuery.load()
Related
I have to call post action method in controller. my razor view is
#using (Html.BeginForm())
{
#Html.TextBox("count")
// here i have many controls
<input type="submit" value="SUBMIT"/>
}
[HttpPost]
public ActionResult Update(string count)
{
// i will do many business related actions
return View();
}
My question is, can i use normal post call by clicking submit button or shall i use ajax post method?
Which method of calling is good in mvc3 and why?
You must know the difference between Normal post and ajax
if you dont want to refresh your page then use ajax i.e you have to update content dynamically and stay on that page , otherwise use normal post
Both methods are fully supported, and the choice depends on your scenario.
You may use Ajax post method if you don't want to reload all your page on submit. Ajax allows you to make some partial refresh, so you refresh only a part of your page WHILE the normal post call loads an entire page(including headers and some other content that may not be changing).
garethb has given an answer in this link http://www.codeproject.com/Articles/429164/Html-BeginForm-vs-Ajax-BeginForm-in-MVC3.
the answer is
Ajax forms are suitable in situations, where you need to do modify or save operations asynchronously , without redirecting to any other forms.
You are using the MVC of ASP.NET, which is server-side. Posts are issued by the Javascript written on the client-side. Posts are supported by your server-side, so you can use "normal" or ajax posts as you like.
This is what I've tried:
In Controller:
public ActionResult Detail()
{
List<string> list = new List<String>();
return View(list);
}
In View of Detail:
#model dynamic
<!-- some HTML -->
<script>
#Model.add(document.getElementById("input_box").value);
</script>
Problem is, intelli-sense is not detecting that the passed in object is a List so #Model.add doesn't work.
My goal is to pass a list into the view so I can gather data from user inputs, and then use that list of data in my controller. Any ideas?
The current problem can be solved by using a statically typed model:
#model List<string>
<!-- some HTML -->
<script>
#Model.Add(document.getElementById("input_box").value);
</script>
However, you'll quickly hit another wall when the compiler complains that it doesn't know what document is. That's because the Razor view is rendered on the server side, but document is an object provided by the browser on the client side. You simply can't dynamically add elements to this server-side list from the client. You'll need to encode your list somehow for the client to be able to read, say in a JavaScript array. Then dynamically modify the list on the client side, perhaps using the JavaScript .push method. And then post this back to the controller in a separate action.
It's not going to be that simple.
Firstly, the Razor statement you used
#Model.add(document.getElementById("input_box").value);
is not a valid C# statement. It will cause an exception when you try to open the page, because the Model type you're using (List<T>) does not have a method called add (though it has Add), and further because that javascript is not valid Razor code either :)
Secondly, you seem to be confused about how the server-client interaction works.
You need to carefully think about how what you want to do can be realized--
For you to be able to get information from the client (the user's browser), you first need to render the page, send it to the client, let the user (or whatever) enter the information, and then get them to send it back to a Controller action. At the moment your page is not even rendering, so you couldn't hope to get data from anyone.
This can still be solved in numerous ways, one of which is to use Html.BeginForm(...) to create a simple form and have an input element and a submit button somewhere in there.
If you can get the user to submit the data back to you, you can then do whatever you like with the data in the previously mentioned Controller action.
Okay, solved it by creating a global array in the JavaScript of the View, then passing it to a Controller function as a parameter with Ajax.
On view page I have loaded content inside tabs. On user click inside tab I'm sending ajax request to the controller which sends back partial view. Everything works and now I want to implement caching on this tab content. So, I want to on first tab call content to be loaded and then cached for 60 sec. and the same for every other tab content.
I tried something like this
[DonutOutputCache(Duration = 60, VaryByParam = "activeTab")]
public ActionResult GetTabData(string activeTab)
{....}
but this display cached content of the first tab in every other tab.
Where is DonutOutputCache attribute defined? Maybe it isn't properly using VaryByParam?
In MVC 3 you no longer need to specify the VaryByParam option on the built-in OutputCache attribute.
[OutputCache(Duration=60)]
public ActionResult GetTabData(string activeTab)
{....}
Should be all you need.
I've never had success using caching Attributes on Actions in my ASP MVC projects. One of my co-workers says that he uses them and it works, but I've always had to either tell a page not to cache using this:
$.ajaxSetup({cache:false});
Or including cache: true / false in my $.ajax
$.ajax({ .... cache: false, ... });
Please refer below links.it's give proper details in output catching
OUTPUT CACHING IN ASP.NET MVC
Improving Performance with Output Caching:
Improving Performance with Output Caching
When we click a Form's submit button, then action of the controller which is having HTTPPost attribute is called but what if i want to call or perform an action when a normal HTML button is clicked
Although following articles
http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H
HTML button calling an MVC Controller and Action method
tells the approach but both of these are using controller name in view, So view has to know about controller, I am looking for an answer that view wont have to know about controller.
because views has to be Independent from Controller, Views should not know about controller
So, if you know the answer then please reply
any form that directs your user to url created by
<a href='#Url.Action("{action}", "{controller}")'> click me </a>
or
#using(BeginForm("{action}", "{controller}")
will do what you want.
That can be with a
form
button link
It's the destination that matters. The View does not "know" anything about the action or controller. The helper does.
To execute an MVC action from the client side (i.e. from a view) you need to hit a URL (with any verb: get, post, put, etc).
Therefore to execute an action form a view you will need to know the URL of that action, by default that URL is directly mapped to the controllername/actionname but you can re-define this if you want to create more abstraction between view and controller.
Given this your button just needs to be a link to a URL or linked to js to do an Ajax http request.
Hope that helps.
You cannot have 2 actions on the same controller with the same name and the same HTTP verb. So what you are asking doesn't make sense. You could invoke the same controller action as the one that rendered the view without specifying an action and controller name. The reason why Html.BeginForm() works without specifying an action and controller name is because the form is sending a POST request to the server and you can distinguish the 2 actions.
I have an "Edit Profile" lightbox on my page, which posts to the controller via jQuery ajax. I handle the response in the jquery end instead of returning a View. However, when the profile is saved, I need to refresh the values on the page displaying the popup. How could I achieve this in MVC2? For example, if the user changes her name and avatar (in the lightbox), after she saves the profile, I'd like to update the avatar and name everywhere it occurs on the page.
Well what i would be doing is make your Controller return a PartialViewResult, which the end result is basically HTML.
The Partial View would be the popup itself, so essentially your calling your Controller method via AJAX, doing your server-side work, then re-rendering the Partial View to the client.
Have the action you post to via jQuery return a success for failure message. If it is a success, change the avatar/name/etc on the page using the values already in the textboxes (i.e.: the values you posted to the controller). If it is a failure message, display the validation errors.
In your jQuery AJAX, everything can be done in the callback function of the AJAX request.
Prabhu - both your profile page (i.e. the 'main' div contained within it) and the popup div should be partialviews. on posting the popup back to the server, you should requery the main page partialview and return the appropriate html, targetting the 'main' div.
that's certainly the approach that i take for a very similar task.