asp.net MVC3 View - asp.net

I want to open a view that says update or insert successful. I did not create an action because I only want a simply status page. I created a view called Status.cshtml
I get a message saying that the view cannot be found. Any idea what is wrong?
Do I need an action for this to work?

When your process has finished successfully use the following line of code...
return View("Status");
Note: There is no action method with the same name - so navigating to www.yourdomain.com/YourController/Status will not route correctly...

Just a View is not enough. Your request first reaches a controller action through Asp.net mvc routing and only if the correct action is found the relevant view is displayed. Here is a video if you need a quick grab of how asp.net controllers and views work.

Related

Server Error '\' in Application

am working on my first MVC 5 application. Unfortunately I have run into an error and after searching the internet for the past 4 hours, cannot figure how to fix it. Basically I keep getting the following error.
I understand what the error means. But my Profile view is supposed to be under the Manage view. Basically when a user logs in they should see a new link that says view my profile. Its location should be /Account/Profile not /Home/Profile.
Do I need to change this in the application routing or should I remove the reference I have to this view in the HomeController and instead move it to the AccountController?
Thanks in advance for the help.
The controller that generates a view doesn't restrict you to only that controller, your links can point to any controller. So in your view when you render the link to the Profile view you should specify the controller name.
In the following example I'm using the overload of ActionLink that lets me specify the controller (i.e. Account) that contains the action (i.e. Profile).
Html.ActionLink( "View My Profile", "Profile", "Account" )
ASP.NET MVC by default checks first for the respective view in \Views\[ControllerDirectory]\, but after that, if it doesn't find a match, then it looks in \Views\Shared folder.
Having a view in the shared directory you can share views across multiple controllers. You can add your Profile view to the Shared subdirectory and you should be fine. If you still prefer to have the Profile view under Account Controller and want to reference from other controller's action then change your return statement to specify the location of the view like below.
public ActionResult ProfileAction()
{
return View("../Account/Profile");
}

Custom message display page?

In an Asp.Net Mvc 4 site, there may be various messages needed be shown to end users (e.g. "Thank you. The document has been submitted.", "The item and its depended items have been deleted.", "Cannot find the row with ID of xxx.", "Cannot delete this row because it's xxx is depended on it.", ... etc).
How to (and what's the best approach) define a generic page accept a message and display it? The page should be able to be used by all the controllers in the site.
Added:
How about create a ShowMessage controller and view which accept Query string/ViewData/TempData and display it. Other controllers redirect the page if needed. Is this a good solution?
I would also recommend this:
toastr
It was created by John Papa, who frequents SO on a fairly regular basis.
You may want to store message in ViewBag/ViewData or TempData and render it to your particular view. You can create extension method in baseController class and hence it will be accessible from all the controller. Check out below blog post that discusses the same.
http://blogs.taiga.nl/martijn/2011/05/03/keep-your-users-informed-with-asp-net-mvc/
You could use a client side notification plugin such as jGrowl or noty.

asp.net MVC 3 action method

I have created a partial view and I am using it in admin controller's Home action method. Inside partial view, I have an Account Controller with action method named Logon. I am trying to access main view's URL like this
if (Url.ToString().ToLower().Contains("/admin"))
return Redirect("/Admin/Index");
but I cant get the required result. How to get it admin/home ?
From the comments above it looks like you want to just access the current url. If so, then you can simply use the Request object. E.g.
Request.Url.AbsoluteUri
I wonder why you need this though... what is it you are trying to do exactly?

asp.net mvc expected controller and action not getting invoked

I have a weird issue. I loaded a page using "LoadMyPageController" and there is an ajax_submit_button1 which can be used submit the page.
Also there is another ajax_submit_button2 to print the page. This button submits the view model of the page as a whole to the "PrintController" which has a "PrintData" action.
Now when I hit the "ajax_submit_button2", my PrintController.PrintData is not invoked. Instead when I check my fiddler tool the request is made as
http://localhost:18484/LoadMyPage/PrintData?Length=14
which is an invalid URL.
I have contructed my ajax_submit_button2 in such a way that it should invoke
http://localhost:18484/Print/PrintData?Length=14
But I don't know why LoadMyPage controller is present in my URL.
Any thoughts or comments would be appreciated. By any chance does asp .net MVC decides that it will take a default controller on its own if it can't find the controller action for any reason.
The code is a kind of tightly coupled so can't post it. I want to know if anyone experienced a problem like this.
This has nothing to do with the routing on the server since the request being made by the client has the wrong controller in it. I suspect that your code generating the url for the submit button is not correct -- i.e., not specifying the controller to be used -- or that you have a form around the submit button that is actually being invoked instead of (or in addition to) the ajax code. Note that if your submit handler doesn't return false, the default form action will be invoked and the form submitted normally. If you do have a form, make sure that the url on it is correct and that your submit handler returns false.
$('#printForm').submit( function() {
$.ajax({
url: $(this).attr('action'),
...
});
return false; // this is important!
});

How to supress re-post when refreshing a page - ASP.NET MVC

I am building a wizard using asp.net mvc. currently when the user hits next (or previous) the form values are posted to an action which does any processing required and then renders the next view.
the problem i am having is that if the users hit refresh in that new view they get prompted to re-post the form values which causes a ton of problems.
In firefox i am getting the message: "To display this page, the application must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."
Is there any way to prevent users from being able to re-post back to the action?
Thanks in advance.
Using the PRG pattern.
http://en.wikipedia.org/wiki/Post/Redirect/Get

Resources