.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

Html.RenderPartial appears to be stripping Model of custom attributes

Posted By:      Posted Date: August 27, 2010    Points: 0   Category :ASP.Net


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

View Complete Post

More Related Resource Links

Custom Attributes + Reflection


I recently started messing around with custom attributes and reflection. In my code below GetCustomAttributes never return a list (it should return atleast one):

Am i defining the custom attrib wrong?

public class GOrder:IOrder
        public string CertProductIDString { get; set; }

        public string GetRESTParams()
            string data = string.Empty;
                System.Reflection.MemberInfo inf = this.GetType();
                object[] attributes;
                attributes =  inf.GetCustomAttributes(typeof(RESTParamAttrib), true);
                foreach (Object attribute in attributes)
                    RESTParamAttrib attrib = (RESTParamAttrib)attribute;
                    data = data + string.Format("\nData: {0}", attrib.Param);

            catch (Exception ex)

                throw ex;

            return data;

    public class RESTParamAttrib:System.Attribute
        public RESTParamAttrib (string param)
            Param = param;

        public string

Basic Instincts: Designing With Custom Attributes


One of the most powerful aspects of the Microsoft® . NET Framework is its support for attributes. Attribute-based programming is extremely powerful because it adds a declarative dimension to designing and writing software.

Ted Pattison

MSDN Magazine May 2005

Model Binding the values in an HTML Table back to Controller / Model


Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back.

In my example, I have a Class called "Ingredient" which has 4 public accessors:  Name, Barcode, Amount, and Unit

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcACE.DomainModel.Entities.Ingredient>>" %>

<% using (Html.BeginForm("SubmitOrder","MyController"))
{ %>

<% if (Model != null)
foreach (var item in Model)
{ %>
<td><%= Html.Encode(item.Name)%></td>
<td><%= Html.Encode(item.Barcode)%></

HTML.RenderPartial vs HTML.EditorFor


This maybe a lame question but I wanted to understand the detailed differences between HTML.RenderPartial and HTML.EditorFor. I am using ASP.NET MVC2 with Framework 4.

Can someone point me to a link?

corrupt html from custom control on win2003/iis6 box


Hello everyone -- first time poster and relative newcomer to the boards.  This bug I'm tackling has me at my wits' end and it's come time to turn to the masses!  If this is posted in the wrong forum and/or has already been addressed, I apologize.  However, I've done thorough searches and haven't come across anything akin to what I'm facing.

I'm working with a website that has a set of custom controls, all descending from a base class.  These controls take DataSets returned from web services and renders custom html via overriding the Render method.  Everything works fine on my local box and my coworker's box (both xp machines running iis 5).  However, on our server (windows server 2003, 64-bit, iis 6), the html rendered by the browser appears corrupt, with garbage replacing individual characters, as follows:


Cannot set custom attributes on mobile controls in this page



I am beginner at mobile Pages with ASP.NET. And i want to make an application with contains  a few TextBox that have different size.

it's my sample code here:

<%@Page Inherits= "System.Web.UI.MobileControls.MobilePage" Language= "C# " %>
<%@ Register TagPrefix= "Mobile" Namespace= "System.Web.UI.MobileControls " Assembly= "System.Web.Mobile" %>
<Mobile:Form runat="server" id="form1" Paginate="true" >
<Mobile:Panel Paginate="True" runat=&

how do I add a custom report that appears in SSMS

Hi, I would like to create and add a custom report that would appear when users click on Reports - Custom Reports at both the Instance and the DB level in SSMS. I know how to create a report but I dont know how to add the report so it appears when users click through SSMS. I would also like to know where is the RDL (if that is what is being used) for the build in reports for either SQL Server 2008 and/SQL Server 2008 R2. thanks, SQLSearcher

Html.BeginForm and Returning Model

Any idea how can View return model(myModelValues) on the click of submit button,aspx code     <% Html.BeginForm("submit", "RenewalManager", FormMethod.Post); %>    <input type="submit" name="submitButton" onclick=" return confirmReject()" value="Test" />    <% Html.EndForm ( ); %> controller code        public ActionResult submit(string submitButton)        {            switch (submitButton)            {  //Cases...                case "Test":                    // call another action to perform the cancellation                     return (Confirm());  //...             }        }        public ActionResult Test()        {  

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

Validating Custom Types and Html.EditorFor() Templates?

I'm finding my self with the problem that if I do not show the Template for a "complex" object I have in the main view object I still get the errors in the summary. How do i get around this? I've got a model with say:   HouseType ->Persons (has its own editor template) ->Pets(has its own editor template)   In the create view I show each one depending if they are not null. But when I try to validate HouseType and say I have Persons not null and pets is null. I get errors of validation for pets. How do I get around this? -Thanks

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

Custom Model Binder Attribute

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

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 Field - SP is creating HyperLinks and using HTML entities

Hello. I'm seeking help with a problem related to a custom field in SharePoint 2010. Our Custom Field builds upon (extends) the SPFieldMultiLineText field. Data saved to our Custom Field is being processed by SharePoint before it is sent to the client. For example < and > is replaced by HTML entities (&lt; and &gt;). Text such as http://bing.com and mail@bing.com is automatically being converted to hyper links (<a href="mailto:mail@bing.com">mail@bing.com</a>). Is it in any way possible to have SharePoint deliver the data as it was stored in the custom field? We don't want it to replace HTML with HTML entites, nor should it create hyperlinks automatically. I have attached our XSL file below. <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Micros
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