asp.net mvc generic/custom views - asp.net-mvc

the routes are as follows
site.com/{section1}/{page1}
site.com/{section1}/{page2}
site.com/{section2}/{page3}
etc.
the pages are 2 column each where the left bar is different for each section
the page content is fetched from the database based on section + page combination
I'd like to make the page markup/layout/css be done/uploaded by the user
Questions:
hopefully this is implemented somewhere? any links?
how much can this be dumbed down?
what/how can checks be enforced so that the few required tags/markup are present/visible?
any gotchas?

figured it out using ActionAttribute

Related

ASP.NET MVC 5 Customise Bootstrap navbar based on User Role

I'm using the ASP.NET MVC 5 built in authentication methods. I would like to show and hide links (in the menu navbar) based on the role the user is in.
Has anyone acheived this?
Where would be a starting point?
Just wrap your links in:
#if (User.IsInRole("SomeRole"))
{
...
}
You can use MvcSiteMap for this. It has a feature called SecurityTrimming which uses the [Authorize] attribute on your action methods to decide whether or not to display the menu item.
I know it's frowned upon to post a links in answers but I found this blog post very useful.
In addition to the role-based menu visibility, I added custom attributes to the MvcSiteMapNodes to determine visibility of links that were accessible to users but I didn't want shown in the menu (e.g. Edit pages), and I also added icon attributes which allowed me to use the bootstrap menu icons e.g:
<mvcSiteMapNode title="Till" controller="Home" action="Index" area="Till" iconClass="icon-home" visibility="true">
I went a bit off-topic there, but I just wanted to highlight how flexible MvcSiteMap is.
Two things I do. Either
User.IsInRole(admin)
{link somewhere}
Or what I personally do is because I use areas I have a viewstart in area admin which links to admin shared viewmodel then in admin shared view that links to the public view.
In the admin shared view. I set up a section. Inside this section I define extra nav details what that specific role will see and add them in a list tag
Then inside public shared view I then use (on phone can't remember exact name something like)
Html.IsSectionDefined
I personally like the second method using areas and sections both would work fine but with the second I find it much cleaner and you can be so much more specific and much simpler

What is the Rails way to split a view into separate pages?

Within my show action/view I'm displaying a lot of data that I want to split-up into separate pages (in this case three pages total). I can do this easily by adding a new action and view for each additional page, but is that the "correct" way to do it in Rails?
Great question!
I can do this easily by adding a new action and view for each additional page, but is that the "correct" way to do it in Rails?
I suspect you are unsure about violating REST?
I don't know what data you are displaying, but In the end the clearest and simplest solution for you (code wise) and your users (design wise) should win, if that means adding a new action, so be it. Avoid adding a new controller just for the sake of a new show action.
Are you looking for a pagination solution? If so, I would suggest either kaminari or will_paginate. Also, they each have railscast if you need any help getting set up.

ASP NET MVC: Dynamically adding or removing inputs on the form - unobtrusive validation

Before starting, I do have a very particular question and if you want to answer it go straight to the end. But I do welcome comments and advices hence the lengthy post.
OK, we deal with a lot of forms and some of these forms are quite lengthy and have many fields. We also have a requirement - in addition to top level fields - to be able to have variable number of repating rows - as we call them. For example, let's think of a customer which has name, surname and age while it can have zero or many addresses (say 0 to 10) so the user must be able to add or remove contacts from the form while filling it in. So typically user gets and "Add" button to add more addresses and next to each address, a delete button. Potentially there could be more than one repeating section in the same form but I am not going there. The point is, because of legal and historical reasons, all the forms must be saved at once so while the forms can be edited, we cannot accept a half-filled form and have another page for users to add and remove addresses, e.g.
I am using ASP NET MVC 2 (strongly typed views with a single generic controller) with client side validation and heavy jquery scripting for flashy features. We are probably going to migrate to ASP NET MVC 3 very soon and I am already playing with 3 for finding a good solution. These addresses are defined on the Model as List<Address>, e.g.
I currently have a working solution for this issue but I am not satisfied with it: I have an HTML Helper that names the add or delete buttons and a bit of JavaScript to disable validation and allow the form to be posted back (even invalid) and since I can find out the name of the button that was clicked, I have all the necessary logic to handle add or delete and works really well.
But I am posting back and the form is reloaded and I am looking for an aletrnative solution. Here are what I can do:
Do everything in the client side. "Add" button will clone one of such addresses and "Delete" button will remove() the element. I only have to rename the indexes which I have done. We were using jquery calendar and it was breaking on the new elements which I have also fixed. But the validation is not working which can probably work with ASP NET MVC but this solution looks like a very brittle one - a house of card which looks great before you add another card.
Post the whole page usin Ajax and then load it back again: This is probably better than my current solution but only slightly.
Use ajax to post the form and get back JSON and use the data to build the elements or remove them: Again a house of card because of extensive client side scripting
Serialize the form and post using Ajax to a particular action and get back only the repating section (as a partial view). The action on the controller can be reused and called from the view itself to return the partial view
OK last one is the one I am working on but there is an issue. ASP NET MVC 3 with unobtrusive validation works only if the form is engulfed in a BeginForm() while my top level view has a BeginForm() but not my partial view. It works well when I call it from the view but not on the ajax call to get just the repeating section.
(Question)
So is there a way to tell ASP NET MVC 3 to spit out validation data atttributes regardless being in a BeginForm() block?? To be honest if this is not a bug, this is definitely an important feature request. I have in fact used reflector to disassemble the code and the condition seems to be there.
Short Answer:
Add this to the partial view:
if (ViewContext.FormContext == null)
{
ViewContext.FormContext = new FormContext();
}
I don't think it is possible using the default unobtrusive libraries supplied. If you look at jquery.validate.js and jquery.validate.unobtrusive.js it looks like it only validates what is inside the form.
There's a few posts about it if Googled and a few work arounds.
I had a similar issue (although much simpler) where I had a validation summary at the top of the page and multiple forms but the unobtrusive javascript would only populate the view summary if its inside the form (jquery.validate.unobtrusive.js line 39 if interested...).
I'm not sure if the validation library is extendible but most things in jquery are so that might be an option if you want to go down that road.
As far a possible solution to your problem I'll put in my 2 cents for whats its worth.
You could have two actions that are posted to. The first action is you post your model with no js validation and all validation is handled in the code - this will catch all user with javascript turned off.
Your second action is you serialized the model. In mvc 3 using the Ajax.BeginForm has an AjaxOption for Url where you can specify an action for the jquery to call (where it serializes the form form you and you can decorate your action with your strongly typed model). Here you can check the model and return a json result and handle this in the javascript.

ASP.NET: Nested master pages - how to pass content up multiple levels?

Does anyone know how to pass Content/ContentPlaceholder information from a page, up through it's master page, to the parent master page?
Like this example, but with content defined in ChildFile (the page), being output in ParentMaster (q matster page one level higher in the nesting).
AFAIK this is not possible. I haven't found a perfect solution to this myself, but usually what works well for a little amount of possibilities (such as a 3-choice navigation bar) is a deriving secondary master pages from the master page and using those in your views. Another solution one is would be using javascript to manipulate the master page's content. A third one would be to isolate the content you want to change from the client page into a separate contentplaceholder and specify that on the client page.
Edit: With the arrival of Razor, this problem is now perfectly solved: Simply put your variables in the ViewBag in the child view and read it in the layout.
Use properties, pass them through and let the main master page decide what to do upon rendering

How do I do very large pagination in ASP.NET MVC?

I am following the NerdDinner MVC application as a guide to do my paging. What if my results are more than 1000 pages, I want to show perhaps the numbers 1 2 3 4 5 .. 10 on the bottom of my page and perhaps something like >> to move to the next set of 10 or 100 pages.
How can I do this in MVC?
I use the implementation demonstrated by Martijn Boland at: http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/
Go for JQuery Datatables. that's the easiest way to solve pagination issues.Return the controller as JsonActionResult and supply it to datatables data.
The remaning will be taken care by data tables. like pagination, search functionality at all.
Check this https://datatables.net/examples/

Resources