.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Custom Model Binder Attribute

Posted By:      Posted Date: September 05, 2010    Points: 0   Category :ASP.Net
Hello,I have the following custom model binder attribute: public class DateTimeAttribute : CustomModelBinderAttribute { public DateTimeAttribute(String date, String time) { _binder = new DateTimeModelBinder { Date = date, Time = time }; } // DateTimeAttribute public override IModelBinder GetBinder() { return _binder; } // GetBinder } // DateTimeAttributeHowever, when I use it as follows:[AcceptVerbs(HttpVerbs.Get), DateTime("Date", "Time")] public virtual ActionResult Create() { // ... }I get the following error:Attribute 'MyApp.DateTime' is not valid on this declaration type. It is only valid on 'class, struct, enum, interface, param' declarations.What am I missing?I am using MVC 3.Thanks,MiguelError 5 Attribute 'FlyOnDreams.Net.Mvc.DateTime' is not valid on this declaration type. It is only valid on 'class, struct, enum, interface, param' declarations. C:\Users\Miguel\Projects\Fly On Dreams Lab\Fly On Pages\Solution\Trunk\Source\FlyOnDreams.FlyOnPages.Presentation\Areas\FlyOnPages\Controllers\UserController.cs 46 46 FlyOnDreams.FlyOnPages.Presentation

View Complete Post

More Related Resource Links

Access Model Validation inside Custom Model Binder.

Hello,Is it possible, inside a Custom Model Binder, to fire "something" that "says" the value is invalid so it gets handled by validation part?Basically, I am getting an exception when the value for the property is invalid.Thank You,Miguel

Html.RenderPartial appears to be stripping Model of custom attributes



I have implemented a custom ModelMetadataProvider so that I can decorate my view models with some custom attributes and everything was working fine until I made use of a Partial View.

The following code in my view works fine: -

<%: Html.DisplayFor(x => x.Results) %>

Results is a List which renders a custom display template and is also decorated with a custom attribute.  Using breakpoints, after the above line and prior to the code within the custom display template, the overridden CreateMetadata method in my custom ModelMetadataProvider is invoked.  If I look at the attributes collection parameter I can see that it does contain my custom attribute thus everything working as expected.

However, if I replace the above with the following line of code in my view then it breaks: -

<% Html.RenderPartial("ApplicationSearchResults", Model.Results, new ViewDataDictionary()); %>

All the Partial View contains is: -

<%: Html.DisplayFor(x => x) %>

Again using breakpoints, after the above line and prior to the code within the custom display template, the overridden CreateMetadata method in my custom ModelMetadataProvider is invoked.  But this time if I look at the attributes collection parameter my custom attribute is not there.

Any idea

Custom Validation Attribute is not called by validation mechanism.

Hello everyone I have create custom validation attribute and assign it to class level validation. Unfortunately, it is not called. I try every way that it think it could be solve the problem. However, it take me for hours and I can't find the attribute is not called by validation mechanism.For illustrate you I put the following code.Attribute[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class BooleanDependencyAttribute : ValidationAttribute { private const string _defaultErrorMessage = "?????????{0}"; private readonly object _typeId = new object(); public string DependencyPropertyName { get; private set; } public string DependentPropertyName { get; private set; } public BooleanDependencyAttribute(string dependencyPropertyName, string dependentPropertyName) : base(_defaultErrorMessage) { DependencyPropertyName = dependencyPropertyName; DependentPropertyName = dependentPropertyName; } public override object TypeId { get { return _typeId; } } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,name); } public override bool

custom model validation

 Hi all, I am trying to wrap my head around a practical real world scenario where the out-of-the-box model validation framework in MVC2 poses some challenges. Consider the following model classes:     public class Organization    {        public int ID { get; set; }         [Required(ErrorMessage = "Work Address is a required field.")]        [DisplayName("Work Address")]        public Address WorkAddress { get; set; }         [DisplayName("Home Address")]        public Address HomeAddress { get; set; }     }     public class Address    {        [DataType(DataType.Text)]        [DisplayName("Address 1")]        [StringLength(50)]        [Required(ErrorMessage = "Address Line 1 is a required field.")]        public string Address1 { get; set; }         [DataType(DataType.Text)] &n

how to read a custom dynamic data meta data attribute in a field template

All --Please help.I need to know how to read a custom dynamic data meta data attribute in a field template.I know how to do it in a page template because I have a handle to the meta table on the page.However, I do not know how to get a handle to the meta column at run time in a field template.Can you help?Please advise.Thank you.-- Mark Kamoski

What is the correct error handling in custom model binding?

Hello,I am working on a Custom Model Binder where a certain error can occur.When this happens it returns null and a error is added to the ModelState.The problem is that in some projects I am using Fluent Validation and the errors messages I am adding through FV are not taking effect.The message added by the Model Binder always prevails.Should the Model Binder add error messages or just bind the field and return null if some problem happened?What would be the correct implementation for this?Thanks,Miguel

Custom Authenticate Attribute with Constructor (Dependency) Injection

  I have a [BeastAuthenticate] attribute on my controller.  The following code works fine but I would like to use Contructor (Dependency) Injection with Unity.  The problem is that the attribute will run the contructor with no parameters.  Interfaces ITMSLogger and IADGroups are setup to use Dependency Injection with TMSLogger and ADGroups respectively.   The following code works fine but doesn't use dependency injection for class ADGroups. public class BeastAuthenticateAttribute : AuthorizeAttribute { private ITMSLogger logger = new TMSLogger(); public BeastAuthenticateAttribute() { ADGroups adGroups = new ADGroups(logger); Roles = adGroups.GetRolesWithGroup("BEAST"); } } I tried the following but I'm getting an error ("Object reference is required...") with "this (adGroups). public class BeastAuthenticateAttribute : AuthorizeAttribute { private ITMSLogger logger = new TMSLogger(); private IADGroups adGroups;// = new ADGroups(new TMSLogger()); public BeastAuthenticateAttribute() : this (adGroups) { // adGroups = new ADGroups(logger); // this(adGroups); } public BeastAuthenticateAttribute(IADGroups adGroups) { //ADGroups adGroups = new ADGroups(l

Custom validation attribute that compare 2 valaues

I want to create Custom validation attribute  that compare 2 valaues i wnat to get the two vlaue   in side the validator together sucha that i have check  2 conditions 1)if the values of the two fieds are 0 then no need to do any comparison else value 1 should be less than value 2 MVC2  

Allow System.Enum and System.ValueType as parameter types in a custom attribute

This is a continuation from a Microsoft Connect posting: https://connect.microsoft.com/VisualStudio/feedback/details/507907/error-an-attribute-argument-must-be-a-constant-expression-etc-incorrectly-received-when-the-argument-is-of-type-enum I am replying to Karel Zikmund’s latest posting, dated 29/07/2010 at 12:57.   Thank you for your response Karel, but once again, I am afraid you are wrong: > > This is irrelevant: the same is true irrespective of whether the parameter type is System.Enum or System.Object. > It is actually relevant - Think how IL code for this code should look like: >    void MyMethod(System.Enum arg) { System.WriteLine(arg); } >    MyMethod(MyInt1Enum.One); >    Mymethod(MyInt4Enum.Two); I’ve looked at the generated IL. It looks exactly the same as it does if I change the parameter type to System.Object. This is also what I expected. What IL did you expect to be generated? > System.Object is passed by reference which is a fixed size (4 or 8 bytes on x86 or x64 arch respectively). Yes, and so is System.Enum (and System.ValueType). > Enums and value types are passed by value (unless they are boxed) and their size therefore has to be known at JIT time. Yes, but System.Enum and System.ValueType are both neither an enum type nor a value type — they are both reference types that

Could not determine the threading model of the Custom Resolver Component from the registry.

I have an application that uses web synchronization and performs merge replication programmatically using RMO. A number of the tables in the publication use a COM-based custom conflict resolver that I have written in c#. I have an installation package that creates a virtual directory containing the replication listener (replisapi.dll) and installs the conflict resolver. This week the package was deployed on a 64-bit Small Business Server (SBS) 2008. The first subscriber was able to synchronize successfully but subsequent syncs and syncs by other subscribers resulted in the following exception: Could not determine the threading model of the Customer Resolver Component from the registry. In the registry on the server the ThreadingModel value of my conflict resolver was set to Both and I was able to resolve the issue by changing the value to Free. My first question is why when using the Both model would the resolver not simply be loaded in the same apartment as the merge agent? My second question is whether I have plumbed for the correct alternative of Free (rather than Neutral or not specifying a model)?

Creating custom ranking model for Extended sharepoint search


Howdy coders, 

I am new to sharepoint search and i am trying to customize sharepoint search for my application using FullTextSqlQuery OM. i am able to fetch the results.i want to add  custom ranking model based on some managed properties which are added to the sharepoint list and document libraries.The search results page which i am designing should display the results based on users context, if the user is on first page then the search results relevant to first page should appear at the top of the search page. if the user trigger search from second page then the search results relevant to second page should display on top. 

To achieve this i want to implement a custom ranking model, can any body help me with the steps for implementing a custom ranking model with some custom managed meta data properties 


Thanks in advance.


How to get field name in the custom attribute?



     How to get field name in the custom attribute?


  [Filter(FilterControl = "Cascade")]
  public object Product { get; set; }

In the codes of FilterAttribute ,How to get field name?(this is "Product")


How can I reuse Model Metadata for custom View Models?



I'm working on an ASP.NET MVC 2 project with some business entities that have metadata dataannotations attributes applied to them (Validation attributes, Display attributes, etc.).

Something like:

//User entity
public class User

[Required(ErrorMessage = "Vorname fehlt")]
[StringLength(MaxNameLength, ErrorMessage = "Vorname ist zu lang")]
public string FirstName { get; set; }

how to add custom button to ribbon using server object model?(not using xml)

how to add custom button to ribbon using server object model?(not using xml)

new custom control using object model?


Is it possible to create our own ribbon ?if possible,

How To add new ribbon and under that new  custom controls to ribbon using server object model?


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend