How would I open an MVC View in a new browser window - asp.net-mvc

I have an ASP.Net MVC application. I would like to create a view that lists the contents of a simple collection and show it in a new browser window.
Is there a way to show a view in a new browser window using a simple link?
I have struck out with Html.ActionLink. The Url.Action below does result in the Controller action being called but does not open in a new browser window.
Open MVC View in New Browser.
Is opening a view in a new browser window possible in MVC?
If so, does anyone know how?

You can specify target=_blank in the HTML properties of your link to open it in a new window. There's a parameter on Html.ActionLink that allows you to specify arbitrary HTML properties to add to your link, like so:
Html.ActionLink("Text", "Action", new { id = 1 }, new { target = "_blank" });

Related

Why can't MVC find my view correctly?

I'm working on MVC5.
I already have my controllers and their respective views. If I click on a view and open it on my browser, everything's fine, however, whenever I start the project normally on VS, my browser opens, for example, this link:
http://localhost:50738/Views/Profile/Index.cshtml
However, whenever I open the view directly I have:
http://localhost:50738/Profile or http://localhost:50738/Profile/Index
At the RouteConfig file I just said that I wanted Profile to be shown by default, instead of home. Why does '/Views/' appear on my browser?
code:
namespace WorkTimeManager.Presentation
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Indicators", action = "Index", id = UrlParameter.Optional } );
}
}
}
Its because you have your Start Action set to current page in your web project. So when you start your web app it will try to load the page you are currently viewing in Visual Studio.
To fix this, right click your web project in your solution and click properties, then click on to Web and you will see Start Action - with Current Page radio button selected.
Change this to Specific Page and then type in your homepage URL e.g. http://localhost:50738/Index then everytime you start your web app from Visual Studio it will open on that page instead of trying to open the current page.
When you click on a .cshtml file ("click on a view"), you're only opening the file that describes that view, you're not opening it via 'mvc'.
MVC doesn't send the .cshtml file to the browser, the controller reads the view and renders it.
So the file in Views/Profile/Page.cshtml may be rendered by a controller action ProfileController.Page() (or may not... but would be in the simplest case) for which the URL would be /Profile/Page
This is a brief summary, there are better explanations available of how MVC works.

MVC 4 load partial view dynamically on single page

I have a MVC 4 application which has a single home view. I have three link buttons and want to load three different forms dynamically based on the button click. I am using mvc partial views. So, if I click on button-1 it should load partialView-1 and should also send value-1 from the corresponding text box to partialView-1.
I am looking for mvc inbuilt approach rather than doing heavy javascript work.
You can do this like this.
A. Have different methods inside your controller returning PartialViewResult
[HttpGet]
public PartialViewResult GetPartialView1(int value)
{
return PartialView("_PartialView1"); //This view should exist in appropriate views folder.
}
B. Your buttons on the left handside should be #Ajax.ActionLink
#Ajax.ActionLink(
"Button1",
"GetPartialView1",
new {
value1 = 1},
new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "righthandsidebox"
}
)
C. The UpdateTargetId = "righthandsidebox" that should be the id of the div on the right hand side. Contents of righthandsidebox will be replace by the PartialView
There is nothing built into MVC to do this as what you want to do happens on the front-end. If you want to be dynamic you have to use javascript.
If you want to avoid javascript then it can't be dynamic. You would wrap each button and value in a form and have it submit its value to the back-end and by what is passed you then you have your view render what partial you want. But this wouldn't be dynamic as want it to be.
I'm not sure what you consider as "heavy" when it comes to javscript? With jQuery what you want you do doesn't take much.
(lighter javascript work) You can either have your View place all of the partial views on the page with a class that sets them to display:none then just javascript to simple change out the class to view the partial you would like. But you have a lot of partials then you can do the following.
Or have the buttons make an `ajax call to the back-end fetching the partial that you need.
The bottom line is... if you want dynamic you need to use javascript.

How to get ASP.NET MVC to open a new window?

I’m trying to put together a MVC app that isn’t a typical create a record and present the record to the user.
I’m finding some things that aren’t friendly in MVC as they are in Web Forms. My view has two dropdowns, a textbox, and a submit button. In one of the dropdowns I have to prepopulate it with codes and description. That part is done.
Next, I have the user enter text into the textbox. They click on a Find button. Find will populate the 2nd dropdown. Without viewstate, the code is a bit different but possible. Next, the user clicks on the submit button. Here is the tricky part. I need input from the view to generate a PDF file, then open a new browser window or tab to display the PDF.
I also want to delete the PDF from the server before presenting it. I can delete the PDF before presenting it in web forms. I found Actionlink can open a new window, but Actionlink doesn’t push the input on the view to the controller. A standard form submit button does, but a submit button doesn’t open a new window. A controller cannot open a view in a new window either.
How do I get the users input and push that input to the controller, and then display the PDF generated into a new browser window? On top of that, I need to delete the PDF off the server.
Simple create an Action that returns your Pdf stream as a FileResult
If you are generating your pdf to a memory stream: (I recommend this):
public ActionResult DownloadPdf()
{
// you need some code here to generate the pdf to the memory stream.
return File(stream, "application/pdf", "DownloadName.pdf");
}
Or if you prefer, use the filepath directly :
public ActionResult DownloadPdf()
{
// get pdf filepath
var path = "Chap0101.pdf";
return File(path, "application/pdf");
}
Then in your html code you can use something like this to open in a new window.
#Html.ActionLink("Download pdf in new window", "DownloadPdf", "ControllerName", null, new { target = "_blank" })
You might return a view with some javascript that first opens a new window and then refreshes to another view. If no javascript is active, the view wouldn't reload and a link would be there to open manually. If you create the PDF in memory, there is nothing you need to delete, just stream the in memory PDF to the client. I'm new to MVC myself, this is just an idea.

How to send parameter in query string on ajax call in asp.net mvc

I want to send the selected page value on the querystring while navigating through paging.
The URL that is generated for paging is like this:
Link/Index?page=2
Link/Index?page=3
But on my URL it only shows Link/Index and performs the Ajax call. But if I disable my Javascript and then navigate through paging it gets Postback and has a URL like
Link/Index?page=2
Which is perfect. But I want this type of URL in an Ajax call as well.
How can I do this? Issue is if we navigate through pages when Javascript is enable it shows Link/Index and when user goes to page no 2 then 3 then 4 and press back button it goes to press page instead of page 3 then page 2.
Here is the code that generates the page links:
<%= Ajax.Pager(
new AjaxOptions {
UpdateTargetId = "divGrid", LoadingElementId = "divLoading"
},
ViewData.Model.PageSize,
ViewData.Model.PageNumber,
ViewData.Model.TotalItemCount,
new { controller = "LinkManagement", action = "Index" }
)%>
This isn't really an issue with the pager, but that's how ajax works. Because you haven't created a new full page request, nothing is stored in history to allow the back button to persist the ajax calls too. You need to use something like jquery.history (http://tkyk.github.com/jquery-history-plugin/) or jquery address api (http://www.asual.com/jquery/address/samples/api/#/section/?id=2.2).

asp.net mvc opening a view in new window without master page?

I am wondering if it is possible to open a view in a new window but without the masterpage defined in the header? would I just define a separate view? or is there a better way?
thanks!
Just create an empty Master page, and pass the name of that Master page when returning a ViewResult:
return View("MyView", "Empty"); // Use the "Empty" Master page to render the view

Resources