How do you embed a razor EditorFor View - c#

I have a composable web application where the user enters an item number and one of a number of processors is invoked to handle the action based on what type of item it is. Each "processor" is in a seperate .dll and can be dropped into the application and picked up by the IOC container to allow the site to be extended.
The processors sometimes need to ask for feedback and so pass up an object that is used along with an Html.EditorFor to collect extra information from the user.
My question is whether or not there is a way to embed an EditorFor template in a seperate .dll and have it get picked up in the web application? Ideally the processor would be able to specify its own EditorFor template so that they don't just use the basic EditorFor template as it is rather lacking.

Related

Best way of sharing user specific variables around my MVC4 app

I am building an MVC4 app using razor, I have done many in the past but I want to roll some best practice in to this one in terms of dealing with variables.
The situation is that I have a logged in user (logged in meaning a windows authentication, with that user name matched to a user in my user table). That user comes with a set of profile options such as "canViewReports", "canEditPerson" etc etc.
Now, there are two prongs to those profile options. First is that my presentation layer needs to customise itself depending on what is presented to it. So profile 1 has canViewReport set to false so the tab for reports will be hidden. Profile 2 has it true so the tab will be shown.
The second prong is that if my savvy users type in /reports/index, I need to pick that up and block access for profile 1 but allow profile 2.
Ok, so at the moment I am using a base controller that is decorated with a [UserDataFilter], that user data filter calls a method that checks the current session for a set of keys and if they are missing assigns them. I put each of those profile options for the current user in to the session. I can then use the session variables in my presentation layer and also in code.
My concern is that that is messy looking in my code having to put this kind of thing everywhere:
(bool)session["canViewReports"] everywhere in my razor.
That lead me to try using the viewstart.cshtml and setting App variables in there that I can use in my razor a bit cleaner. In viewstart I set:
App.canViewReports = (bool)HttpContext.Current.Session["canViewReports"];
I can then just use App.canViewreports everyhwere in my views without too much trouble.
My questions are:
1) Is App.canViewReports for the entire application at an IIS level, or does each connection to IIS get its own pool of App. variables. The thing I want to avoid is the first user setting the variable and every other user that subsequently uses the application getting that value! (on different computers)
2) Is there a better way of doing this!!!
Many thanks
Iain
I would use User.IsInRole("canViewReports") in my razor logic to hide and show the menus item. If you build you menu in you layout you only need to do this once.
I would then further protect the action method by decorating the method with
[AuthorizeUser("canViewReports")]
You could create an ISessionService that stores the session information you need.In this way,you can implement it however you want and have full control over it.It could be retrieved easily via a DI container and it's easy to mock.

.net Replace text with username at runtime

I am writing a C# .net website for management use within my company. On this page, I have a location on the main page to hold notices, which are contextually colored information panels. On the options page, you are given the ability to create them by filling out a form containing the notification's title, message, and a drop down for its class.
One of the the options I would like to provide for the users is to use {name} within the message to show the username of the person viewing the notification. I attempted to use message.Replace("{name}", "<asp:LoginName ID=\"LoginName1\" runat=\"server\" />");, but this resulted in that exact string being posted as opposed to the .net parser converting it into the username.
I am using the default asp.net user creation engine and database, though I've also heard that Silverlight is now the standard.
As for my questions, would I be better off rebuilding the project in Silverlight, assuming Silverlight has the capability to handle secure logins? Also, what would be the correct way to go about replacing the username? Would I be better off with something like {user=#} where # is the user's ID? Theoretically, it would just be a database query from within my Notice class (the constructor calling a method that replaces bbcode with its html counterpart).
Thank you for your help
<asp:LoginName ID=\"LoginName1\" runat=\"server\" /> will work only if directly put in the ASPX. If you want to dynamically set the name without using a control, there's two way:
From the page's code-behind, you can call this.User.Identity.Name
From anywhere in the app, you can call HttpContext.Current.User.Identity.Name

Resource files as editable content in a MVC application

A client wants to be able to add any language to the multilingual MVC-based site I'm currently building.
They would be responsible for providing the translations themselves and while the process doesn't need to be enabled through a GUI it needs to be simple enough, so recompiling the project is a no-go. Editing text files (possibly XMLs) is OK.
Using a semicolon-separated list of language codes in the Web.config file (for example: en-US;de-GE and so forth) and using RESX files inside the App_GlobalResources folder I am able to satisfy the conditions... at least as long as the resources are used inside the Razor pages or MVC controllers.
There is a problem, however - any attempt to use the resources in MVC view model classes (such as the Display attribute or the Compare attribute) throws an error:
System.InvalidOperationException: Cannot retrieve property 'Name' because localization failed. Type '[Resource Type Here]' is not public or does not contain a public static string property with the name '[Resource Name Here]'.
There are several questions dealing with this already on StackOverflow, but ALL of them revolve around turning the resource files from content files into embedded resources. While this might make the code function it's no good for me because the resources can no longer be edited by the client!
What else can I do in this case?
In the end I scrapped using the automated mechanisms of MVC, using #Resources.MyResource.DisplayName in Razor files along with properties such as data-val-required added which can also be supplied with resources.

Kentico CMS - How to differentiate between the same BizForm on two pages?

I have two landing pages with the same exact BizForm. How can I tell if a user has submitted the form on page A vs. page B - testing page design effectivness? I want to keep the BizForm exactly the same; otherwise, I'll end up with a bunch of forms to manage. I don't have access to the web part code, so it would have to be a solution I can do through the CMS GUI.
Using Kentico v5.5.
Look at using an Alternative Form. It's basically a copy of the same BizForm, and you can modify/change a few of the fields. Create one extra field on the BizForm, and assign the value of it based on which form you're using (main or alternate). Just don't display the value on the form itself.
Then, on the page, specify whether to use the main BizForm, or the Alternative BizForm.

Best way to show account information in layout file in MVC3

I am using MVC3, and have a Layout file that is common for all of my Views. In the layout file, I would like to show some information regarding the currently logged in user.
I got it working now, but the only way I found out how to is to set some ViewBag fields in the actions methods, and pick them up in the layout file. This means that I will have to the the ViewBag fields from all my action methods, or at least make a method that sets them, and call this method from all my action methods.
Are there any central way to get this done? The absolute best is to do it once for the layout file in one place, but if no other option one place per controller might be good enough.
The right way is to call Html.RenderAction() inside the layout file, where you want the user-specific details. Then create a suitable Action method somewhere which reads the user's account details and returns them as a view (partial view) or as raw html (return Content("...")).
That way, you can also benefit the ability to cache the layout page and keep the user's account details region uncached (since it's obviously different for every user).
Setting a ViewBag is not a good idea, it's not strongly type and it breaks the correct schema of MVC.

Categories

Resources