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.
Related
I am currently learning JSF and was rather amazed and puzzled when I realized that whenever we use <h:form>, the standard behavior of JSF is to always show me the URL of the previous page in the browser, as opposed to the URL of the current page.
I understand that this has to do with the way JSF always posts a form to the same page and then just renders whatever page the controller gives it back to the browser which doesn't know the page location has changed.
It seems like JSF has been around for long enough that there must be a clean, solid way to deal with this. If so, would you mind sharing?
I have found various workarounds, but sadly nothing that seems like a real solid solution.
Simply accept that the URL is misleading.
Append "?faces-redirect=true" to the return value of every bean's action and then
figure out how to replace #RequestScoped with something else (Flash Scopes, CDI conversation, #SessionScoped, ...).
accept to have two HTTP round trips for every user action.
Use some method (e.g. 3rd party library or custom code) to hide the page name in the URL, always using the same generic URL for every page.
If "?faces-redirect=true" is as good as it gets, is there a way do configure an entire application to treat all requests this way?
Indeed, JSF as being a form based application targeted MVC framework submits the POST form to the very same URL as where the page with the <h:form> is been requested form. You can confirm it by looking at the <form action> URL of the generated HTML output. This is in web development terms characterized as postback. A navigation on a postback does by default not cause a new request to the new URL, but instead loads the target page as content of the response. This is indeed confusing when you merely want page-to-page navigation.
Generally, the right approach as to navigation/redirection depends on the business requirements and the idempotence (read: "bookmarkability") of the request (note: for concrete code examples, see the "See also" links below).
If the request is idempotent, just use a GET form/link instead of POST form (i.e. use <a>, <form>, <h:link> or <h:button> instead of <h:form> and <h:commandXxx>).
For example, page-to-page navigation, Google-like search form, etc.
If the request is non-idempotent, just show results conditionally in the same view (i.e. return null or void from action method and make use of e.g. <h:message(s)> and/or rendered).
For example, in-page data entry/edit, multi-step wizard, modal dialog, confirmation form, etc.
If the request is non-idempotent, but the target page is idempotent, just send a redirect after POST (i.e. return outcome with ?faces-redirect=true from action method, or manually invoke ExternalContext#redirect(), or put <redirect/> in legacy XML navigation case).
For example, showing list of all data after successful editing, redirect after login, etc.
Note that pure page-to-page navigation is usually idempotent and this is where many JSF starters fail by abusing command links/buttons for that and then complain afterwards that URLs don't change. Also note that navigation cases are very rarely used in real world applications which are developed with respect to SEO/UX and this is where many JSF tutorials fail by letting the readers believe otherwise.
Also note that using POST is absolutely not "more secure" than GET because the request parameters aren't immediately visible in URL. They are still visible in HTTP request body and still manipulatable. So there's absolutely no reason to prefer POST for idempotent requests for the sake of "security". The real security is in using HTTPS instead of HTTP and checking in business service methods if currently logged-in user is allowed to query entity X, or to manipulate entity X, etc. A decent security framework offers annotations for this.
See also:
What is the difference between redirect and navigation/forward and when to use what?
JSF implicit vs. explicit navigation
What URL to use to link / navigate to other JSF pages
Bookmarkability via View Parameters feature
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
When should I use h:outputLink instead of h:commandLink?
Creating master-detail pages for entities, how to link them and which bean scope to choose
Retaining GET request query string parameters on JSF form submit
Pass an object between #ViewScoped beans without using GET params
I have Homepage presenter i want to view what inside it in homepage. how can i open it in webaddress ? i already try to open it
with this http://localhost/sandbox/www/homepage/action it wont open
What the different between method action and render.. forexample i have this two method…
actionOut() and renderOut() in the Homepage Presenter when i route it it nette cannot make any different between two..
This depends on your routing. The default one allows you to access all presenters by pattern <module>.<presenter>/<action>. So the question is if the Homepage presenter is in any module, what's the routing, ... You can take a look at debugbar, where you can find a routing panel that may help you understand which route was matched. Also, you didn't specify, what is the exact error.
I encourage you take a look at the lifecycle of the Presenter. Basically, the action method is run always, even if you redirect in a signal method. The render method is run only when the presenter is going to render the current action. The action method should take care of getting the primary resource and "storing" it to the presenter's property, not the template! The render method takes the resource and pushes it to the template. If the resource method is not available, you should call $this->error() in your action method.
I want to write a DotNetNuke module that can take an HTML form and parse or transform it into an asp.net form that would then do a HTTPPost to the page specified in the HTML Form's action property.
We regularly run into the need to use pre-existing forms (from existing sites and Service Providers like Paypal and Constant Contact). Currently, we either use an IFrame, manually convert the form into an ASP.NET user control, or use a forms module to recreate the form. It seems like it should be fairly easy to create an automated way to handle these with ASP.NET.
My quick plan:
1.) Admin Users will paste the form into a settings page and then click a convert button
2.) The code will parse the HTML and generate a ascx User Control from it and store the posting address. We may just have to add runat="server" into each of the form controls
3.) The admin user will be able to specify a variety of response codes and corresponding messages. (i.e. 1 -> "Thank you for your Donation", 2-> "We were not able to process your request at this time due to ...")
Users would then fill out the form and hit submit. The system would get the names and values of all form controls and Silent Post that to the posting address and get the response code and then display the corresponding message.
Any thoughts or suggestions of the best way to do this? Are there any tools that already do this or would be helpful?
Thanks,
David O'Leary
I have an ASP MVC form which updates a business object and then its action goes to another page.
Both of these pages have a textarea with an id "Description", when it goes to the other page, for reasons unknown it fills in the values which were entered before the form submission.
Which method are you using to update your object? Have you tried adding the object type name in the ID?
Like, Product.Description instead of just Description.
Not knowing what your doing exactly, I'd say there is a 'Description' property on the ViewData's Model. ASP.NET MVC will try to match up your form values with values from the ViewData, including the Model. You can rename the control or reference the object directly as Chad said. eg Product.Description
With more details, someone may be able to help more.
Without seeing your controller actions this is a bit of guess work but how does it go to another page?
If in the post controller you return a different view (or return another action that returns a view etc) it may be that the ModelState is using the attempted values from the previous form submission, this is the expected behaviour (it's how the validation system and model binding works for a start). If you are sending them to a different form use a RedirectResult to your next action and use TempData if needed for any state you need to keep while transitioning.
If at all possible I'd also recommend using the strongly typed helpers such as Html.TextAreaFor(x=>x.Description)
<p><label>Description</label><br /><br /><%=Html.TextArea("Description", new { style = "width:470px;" })%></p>
For clarification, I'm going from an "Edit project" page, to a "Add issue" page
I am working on an ASP.NET MVC application that contains a header and menu on each page. The menu and header are dynamic. In other words, the menu items and header information are determined at runtime.
My initial thought is to build a base Controller from which all other controllers derive. In the base controller, I will obtain the menu and header data and insert the required information into the ViewData. Finally, I will use a ViewUserControl to display the header and menu through a master page template.
So, I'm trying to determine the best practice for building such functionality. Also, if this is the recommended approach, which method should I override (I'm guessing Execute) when obtaining the data for insertion into the ViewData.
I'm sure this is a common scenario, so any advice/best-practices would be appreciated! Thanks in advance!
EDIT:
I did find the following resources after posting this (of course), but any additional anecdotes would be awesome!
http://www.singingeels.com/Blogs/Nullable/2008/08/14/How_to_Handle_Side_Content_in_ASPNET_MVC.aspx
How do you use usercontrols in asp.net mvc that display an "island" of data?
Depends on where your information is coming from. We have standard view data that we use to generate some of the information we have on screen that we create in just this fashion. It works well and is easily maintained. We override the View method to implement strongly typed view names and use this information to retrieve some of the data that the master page requires as well.
You could write a helper extension to render the header/menu
That way you could have it show in different places in the view should you need to, but only one place for maintenance.
public static HtmlString MainMenu(this HtmlHelper helper)
Use a base controller class to implement generell filter methods. The controller class implements some filter interfaces IActionFilter, IAuthorizationFilter, IExceptionFilter and IResultFilter which are usefull to implement some common behavior for all controllers.
If the menu data is the same on all pages but different for each unique user.
Generate the menudata in an OnAuthorization or Initialize method of your controller base class. First will be called on authorization. Initialize will be called before every action method. You have access to ViewData Context. Generate the menudata there.
Put the view content for menu and header into the master page and access generated ViewData there.
I tackled a similar design challenge a couple months ago - implementing a breadcrumb feature that changes as user navigates from page to page.
I overrided the OnActionExecuting method to gather the breadcrumbs and store them in ViewData (I use the name of the action as the breadCrumb of the view). Then I updated the Master page to include a user control that takes the ViewData and renders the breadcrumbs.
One thing to be aware is that if you were using the default ASP.NET MVC error handling attribute [HandleError] and your error page is using the same Master page that attempts to read the ViewData, you will soon find out that you can't access ViewData from your error page and it will raise an exception. Depending on whether you need the ViewData for failure scenarios, the viable solution is to use a separate Master page or do this: How do I pass ViewData to a HandleError View?
I'll answer your question with another question. Will the base controller have to determine what type it really is in order to generate the proper menu data? If so, then you're defeating the purpose of polymorphism and the code to generate the data should go in each controller, perhaps in OnActionExecuting if the menu is the same for all actions. Pushing it back down into a parent class seems likely to end up with some switch statement in the parent class doing what each derived controller really ought to take care of.