40154

ASP.NET MVC add HiddenInput attribute to Model in DLL

I'm working on an asp.net MVC 3 application which is using Data Models from a compiled library. However I would like to be able to add the following declaration to some data model properties in the compiled dll:

[HiddenInput(DisplayValue = false)]

The problem is that I don't have the source for the DLL and the author doesn't want to introduce a dependency on System.Web.Mvc. Is there a way, using partial classes or something like that, that would allow me add this attribute?

Mark

Answer1:

No, there is no way. Attributes are baked in the metadata of the assembly at compile time and existing classes cannot be modified at runtime. As far as partial classes are concerned they only work within the same assembly.

Also if the authors of this assembly don't want to introduce a dependency in their library with System.Web.Mvc they probably have reasons for this. Obviously, you, as an MVC developer should use view models which are classes specifically tailored to the needs of your views and then map between the domain models (stuff that comes from different libraries, ...) and view models. Then you would pass those view models to the view and not the domain models. Of course your view models will have all the necessary metadata and formatting such as DisplayName, Hidden, ... To ease the mapping between those two classes you could use AutoMapper.

Answer2:

You could map your compiled library Data Models to a set of local models.

If you were to map your compiled library Data Models to your own set of local models you could do what you like.

You can do this manually or look at a tool like AutoMapper.

Answer3:

Add reference to system.web.mvc to your class project.

Recommend

  • RDF - Distributing rdf:type to all items in the list
  • matlab/octave random event ode45
  • How does extglob work with shell parameter expansion?
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • Reloading table causes flickering
  • How to bind comma separated list of values to List
  • Can you build a truly RESTful service that takes many parameters?
  • How to force Composer to download a local package?
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • Available space left on drive - WinAPI - Windows CE
  • jquery validation - waiting for remote check to complete
  • Translating C# to PowerShell in InterIMAP
  • Mixing WebForms and MVC: What should I do with the MasterPage?
  • How do I formally document a C# Attribute in UML?
  • xcode don't localize specific strings
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Webgrid not refreshing after delete MVC
  • MVC3 Razor - ListBox pre-select not working
  • Very simple C++ DLL that can be called from .net
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Control modification in presentation layer
  • ilmerge with a PFX file
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Read text file and split every line in MSBuild
  • Counter field in MS Access, how to generate?
  • SSO with signing and signature validation doesn't work
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • File not found error Google Drive API
  • How does Linux kernel interrupt the application?
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time