C# Override DisplayAttribute vs LabelFor - c#

I initially have used the answer to one of my previous questions as a helper to override the LabelFor methods. While it was doing exactly what I wanted, it did not satisfy the objective of "automatically modify functionality over the entire project without going through every page to add extra parameters" (, "*" for each LabelFor line in my case).
What I am thinking currently is either modifying LabelFor without adding extra parameter at the end, OR modifying the DisplayAttribute from DataAnnotations without renaming it and using my class name instead of Display.
When I tried to do LabelFor without adding extra parameter (again, refer to my question), no matter what I did, debugger automatically stepped out LabelFor without ever going into my LabelFor method, and was using default LabelFor.
When I tried Modifying DisplayAttribute , it only did what I wanted when I changed it from Display to myDisplay.
I used various sources, and I was getting mixed results, so I will ask here: Is there any way to modify/override either LabelFor or DisplayAttribute WITHOUT adding new parameter or renaming method respectively?
Thank you very much in advance!

No, there is not. In order to use method with the same name, it must have different parameters. Or just rename the method and use it from now on.

Related

Get content block without HTML

Is there a built-in Sitefinity method for displaying blog post content without the HTML? NOT the standard standalone C# methodology, but a built-in method in Sitefinity so I can avoid duplicating functionality.
A built in method in Sitefinity, not a generic C# solution.
I'd like to show a summary from my content rather than the separate summary field. I'm in my custom blog list .cshtml file, doing this to retrieve content:
Html.Raw(item.Fields.Content).ToString()
Which gives me the content, but I need to display with without the HTML tags a user may have added.
If you refer using Telerik.Sitefinity.Utilities you will get an extension method StripHtmlTags for the string type.
This is a public extension method and you should not have problems using it.
I see there are a few classes in Sitefinity that do HTML stripping:
public static HtmlStripper in Telerik.Sitefinity.Modules.Newsletters.Composition namespace.
and
internal static class HtmlStripper in Telerik.Sitefinity.Utilities.HtmlParsing namespace, which seems to be the one you need, because it has a StripHtml method, but you cannot use it as it is internal.
I agree with #Zohar that in this particular case you should just use a standard C# approach - you cannot go wrong with it.

Two Grid Packages Installed: The call is ambiguous between the following methods or properties

I inherited a rather old MVC project that had a Grid package installed, Grid.MVC. It is used extensively, and taking it out or replacing it is not an option (client won't pay for it.)
We just built out a new portal section to the site, and in it, we used a new (and better) grid, NonFactors.Grid.Core.MVC5. Much more features and options.
But here's the problem. In all the places where the old grid is used, I now get this run-time error:
The call is ambiguous between the following methods or properties: 'NonFactors.Mvc.Grid.MvcGridExtensions.Grid(System.Web.Mvc.HtmlHelper, System.Collections.Generic.IEnumerable)' and 'GridMvc.Html.GridExtensions.Grid(System.Web.Mvc.HtmlHelper, System.Collections.Generic.IEnumerable)'
This should be a simple fix. All I need to do is tell the old grids which one they are. But i'm not figuring out the syntax. In the view's, they all have a #using which points to the correct (old) version.
#using GridMvc.Html
....
#Html.Grid(Model.Leads).Named("userGrid").Selectable(false).Columns(c =>
{
....
}
A pic might be more helpful:
I've tried various means of a full path in the view, but the syntax is never right...
#GridMvc.Html.GridExtensions.Grid(Model.Leads).... // Nope
#Html.GridMvc.Html.GridExtensions.Grid(Model.Leads).... // Nope
etc etc etc.
Again, this is probably simple. I'm just not getting it.
Try passing the HtmlHelper instance as first argument to the extension method:
#NonFactors.Mvc.Grid.MvcGridExtensions.Grid(Html, Model.Leads)
#GridMvc.Html.GridExtensions.Grid(Html, Model.Leads)
Extension methods are callable as standard static methods, but just take the parent type as a first parameter, so you should be able to pass the HtmlHelper as so:
#GridMvc.Html.GridExtensions.Grid(Html, Model.Leads)
Disable one of the grid templates from the web.config
in the views folder.
-->

Auto-enumerate Results View

So, I've just discovered the wonderful DebuggerDisplay attribute, and have started using it in my code. I've noticed something really quirky about it, though, or perhaps the issue is actually with KeyedCollection.
Without a DebuggerDisplay attribute, I get "Count = x" as the primary display for a KeyedCollection, and that expands to show the elements of the collection. Adding the attribute, however, I get my custom text, as expected, but it now expands to show the various values and properties that I've added to the object itself. That's great, and makes far more sense to me than the collection-->Raw View style that shows up by default. Now, though, the Results View isn't populated by default, it instead has the customary "Expanding the Results View will enumerate the IEnumerable".
How do I get rid of that behaviour? I know the results are safe to enumerate, and would like them to be enumerated automatically. For bonus points, can I have it auto-expand the selection as well?
As I said in my comments, the closest you can be to what you want is by using the DebuggerTypeProxyAttribute. Unfortunately it's a sealed type so you might not be able to make it easy to reuse.

How would one write an Attribute that would set the default value of an auto-property

I've read the post for setting default properties via an attribute, which end with DefaultValue is for Design Mode or Serialization.
BUT, is there a way to write an Attribute that will do what these posts require: default the property to some value.
If there is a way -- how would one start writing such an attribute?
Thanks,
L-
You can't, basically.
You can set the default in the constructor, though.
As it happens I did implement something that did this very recently, but that was using factory-based construction; the factory checked for [DefaultValue] and set the value via reflection. But attributes can't cause arbitrary code execution unless you use a re-writer like PostSharp.
If the constructor is too far away for your liking, you will have to use a field-initializer and write the get/set against the field.
Unfortunately attributes are just metadata, meaning they cannot run or do something on their own.
However nothing prevents you from writing an extension method with a name like SetDefaultValues that reads default values from attributes and assigns them to properties.
I've done a similar thing in a recent project, and it proved to be a good decision because it kept all default values defined in a declarative style in a single place.
There is an interesting article on CodeProject peering into different strategies for implementation of [DefaultValue]-based initialization and comparing their performance. I suggest you check it out.

Html.BuildUrlFromExpression with reference type parameters for action

I hope my terminology is right. Edit if not.
From my Linq2Sql classes I have a Color class.
One of my controller's actions accepts an instance of this Color class.
I want to create a link to this action so I use
<%=Html.ActionLink<ColorController>(c=>c.Details(ViewData.Model.ActiveColor), "test")%>
Where ViewData.Model.ActiveColor is off course the aforementioned instance of Color.
This renders as follows:
/Color/Details?color=-
Not exactly what I had in mind off course..
What am I doing wrong? Should I start creating custom ModelBinders?
EDIT
I have found where the "-" came from. Apparantly the ActionLink is calling upon ToString. This is rather weird since I see tostring more of way to display something then to identify something (isn't that what GetHash is for?).
Even after implementing the Iserailizable interface on my object, it's still using the ToString method. Can I define somewhere how to serialize my class rather then using the ToString()? I'm feeling like I'm completely on the wrong track.
Actionlink is indeed calling upon ToString(). I ran into the same problem using dates. In my urls I wanted dates to be dd-mm-yyyy instead of the default DateTime.ToString().
First option is to override your Color's ToString() method. If that isn't possible (as with DateTime) there are other ways, but they're a bit "hacky"...

Categories