Closing C# ASP.Net MVC4 Application with a Close Button - c#

I am going to ask the dumnest question ever. I am creating a C# ASP.Net MVC4 application. I want a button on the index page that will shut the application down and close the browser. How do I do it? I currently have the following code snippet in my Index.cshtml to give a button:
#using (Html.BeginForm("CloseApplication", "Home", FormMethod.Post))
{
<input id="close" name="closeButton" type="submit" style="width:170px" value="Close Application" />
}
I am going to a CloseApplication ActionResult method in my HomeController.cs which currently looks like:
public ViewResult CloseApplication()
{
// Something to do here I think
return null;
}
What do I do here?
Am I over complicating things?
Many thanks to all of you who contribute regularly and help us out.

Honestly think you are over-complicating things for yourself.
HTTP is stateless so by the time you have rendered a page onto the client's browser, your connections, etc, should have already been Dispose'd and the instance of the controller that returned the view the client is now looking at should no longer exist. The next request the client makes will spin up a whole other controller instance.
So the user can already "close" their "instance" (from their perspective) of the application by closing their browser - adding a button to do this is kind of pointless IMHO!

Related

Controller's action is getting called twice while loading a view

I am working on an asp.net core application where I am loading a view on click of a hyperlink. The view loads fine but what strange thing happening here is whenever I click on the hyperlink, call to controller's action goes twice. I accidently found this issue when I put debug point on the action method and it got hit twice. Then I also checked the network traffic tab in the browser, there also I noticed 2 calls to controller action got recorded twice.
Markup of hyperlink in layout page:
<li><a asp-action="ApplyLeave" asp-controller="Home">Apply Leave</a></li>
Controller's action method:
[HttpGet]
public IActionResult ApplyLeave()
{
.......
.......
.......
return View();
}
ApplyLeave.cshtml
#{
ViewData["Title"] = "Apply Leave";
}
<header class="page-header">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<h2 class="no-margin-bottom">
Apply Leave
</h2>
</div>
</div>
</div>
</header>
Screenshot of network traffic captured in browser:
Kindly help me in resolving this issue, thanks in advance!
If you see your request in console window on network tab, there is two kind of request:-
1: Doc Type
2: XmlHttpRequest
Doc type request is coming from the View which is action link where action method name is mention, but if you see another request then, this request comes from Javascript environment.
Please see the official definition of XMLHttpRequest:-
XMLHttpRequest (XHR) is an API in the form of an object whose methods transfer data between a web browser and a web server. The object is provided by the browser's JavaScript environment.
Please validate once your JS part.
Thanks you all for your kind attention on this.
I have site.js where I am keeping all the script code and code for ajax calls.
There was an ajax call which was intended to be called on a particular page, but due to improper code, that ajax call was triggering after every other page load.
So I corrected the code for that ajax call and now I am not seeing any duplicate call to controller, everything is working fine.
Thanks everyone.

Sending data to header of _Layout.cshtml regardless of what page the user is on

I have a notifications badge in the header of my _Layout.cshtml file. This is so I can constantly show the user their notifications regardless of what page they are on.
Notifications <span class="badge">#ViewData["NotificationCount"]</span><br>
However, I am wondering if there are better ways to do this. It seems like I need to do the following at the top of every single one of my views:
#{
ViewData["ReminderCount"] = Model.Notifications.Count();
}
This also means I need to have a list of notifications as part of every single page ViewModel throughout my application.
Am I missing something here? I feel like there has to be a better way to handle things like this in ASP.NET MVC.
The ideal thing would be to load the user's notifications one time as soon as they login and go to their dashboard and then continue to show that number across all pages unless they check the notifications, then the number clears.
What you can do is create an action and against that action create a partial view and in the _Layout.cshtml call it, this will save you from code duplication and ViewData :
You action code will look like:
public ActionResult Notifications()
{
var model = Model.Notifications.Count();
return View("_NotificationsPartial",model);
}
your partial view _NotificationsPartial.cshtml would be like:
#model System.Int32
<a href="#">Notifications
<span class="badge">#Model</span>
Now you just need to call it in your _Layout.csthml:
#Html.Action("Notifications","SomeController")

ASP.NET MVC post to controller action from same controller

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.

Trouble loading a partial view from a different controller with model data

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.

MVC 4 HomeController constructor and action always fired

I've been looking for a solution for 2 days now. Maybe I'm mind blocked so I have to ask for help.
I have a project in MVC 4 which has many controllers, models and views. I kept the HomeController and used the Index action as the default entry for the site. There I have a grid, something like this:
<div class="ui-grid-b home-icon-grid">
<div class="ui-block-a"><div class="home-icon" style="background-color:#b13a3a"><i class="fa fa-3x fa-shopping-cart"></div><div class="icon-title">#Mobile.Resources.Resources.resCart</div></div>
<div class="ui-block-c"><div class="home-icon" style="background-color:#1c70ef"><i class="fa fa-3x fa-tags"></i></div><div class="icon-title">#Mobile.Resources.Resources.resItems</div></div>
<div class="ui-block-c"><div class="home-icon" style="background-color:#6b6b6b"><i class="fa fa-3x fa-gear"></i></div><div class="icon-title">#Mobile.Resources.Resources.resSettings</div></div>
</div>
As expected, every time I start debugging, I can see the method
public ActionResult Index() {code}
in HomeController.cs is fired.
Also, in Fiddler I see the following at the beggining:
Host: localhost:55883
URL: /
(Sorry I don't post images, I don't have enough reputation yet)
So far so good. Now, when I click on one of the links in the grid, let's say "Items". I expect to see the Products list, and I do, but if I'm debugging, I see that the Index method in the HomeController is fired as well. An then the Category method in ProductsController is called.
And not only that. Let's say I'm seeing the Products list, and I click in some other link (which go to other view, I've tested many cases) the HomeController Index() is called too.
In Fiddler, when I click the Items link, I see first "/" in the URL column, and then the "Products/Category/ROOT" one.
Following the example, in that view, I clicked over a link which go to "Search/Result", in Fiddler I see "/" and then "Search/Result?productCategory=..."
This happens in all over the site. Every time I click on an anchor, Home/Index is called first, and then the selected path.
I even tried using a simple anchor:
Google
And again, first the Home/Index, and then it redirected to Google.
I suppose it could be related to Route Configuration, or maybe there's something with POSTs being made. Maybe is the normal flow in the mvc life cycle and I'm freaking out over nothing, hehe.
Any ideas? Thanks in advance. I hope I was clear enough.
Many modern browsers "preload" linked pages. So when you load a page, it may also load pages that are linked to those pages. So if you have links back to your home page, then those pages might be loaded by the browser to improve performance.

Categories