:::: MENU ::::
Browsing posts in: ASP.NET MVC

ASP.NET MVC Beta – Setting properties on ViewControls

In ASP.NET MVC Beta, it isn’t possible to set properties on partials when calling them with Html.RenderPartial.

Rusty Zarse blogged about a useful ViewData helper class, which allows you to set properties by passing values to the partial through the ViewData.

I’ve extended this slightly to enable the following syntax:


Which sets properties on a ViewUserControl like this:

     public partial class YUIDataTable : ViewUserControl
    {
        public string ConfigNamespace { get; set; }
        public string DataTableId { get; set; }
        public bool HideFilter { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewDataDictionaryBuilder.SetPropertiesToViewDataValues(this);
        }
    }

Here is the full helper code.

using System;

namespace MvcHelpers
{
    ///
    /// With thanks to http://www.vitaminzproductions.com/technology-blog/index.php/2008/11/12/setting-properties-using-aspnet-mvc/
    ///
    public static class ViewDataDictionaryBuilder
    {
        public static System.Web.Mvc.ViewDataDictionary Create(object data, ModelType model) where ModelType : class
        {
            return (System.Web.Mvc.ViewDataDictionary)CreateInternal(new System.Web.Mvc.ViewDataDictionary(model), data);
        }

        public static System.Web.Mvc.ViewDataDictionary Create(object data, object model)
        {
            return CreateInternal(new System.Web.Mvc.ViewDataDictionary(model), data);
        }

        public static System.Web.Mvc.ViewDataDictionary Create(object data)
        {
            return CreateInternal(new System.Web.Mvc.ViewDataDictionary(), data);
        }

        private static System.Web.Mvc.ViewDataDictionary CreateInternal(System.Web.Mvc.ViewDataDictionary dictionary, object data)
        {
            AddPropertiesToViewData(dictionary, data);
            return dictionary;
        }

        private static void AddPropertiesToViewData(System.Web.Mvc.ViewDataDictionary dictionary, object data)
        {
            if (data == null) return;

            System.Reflection.PropertyInfo[] properties = data.GetType().GetProperties();

            foreach (var property in properties)
            {
                dictionary.Add(property.Name, property.GetValue(data, null));
            }
        }

        public static void SetPropertiesToViewDataValues(System.Web.Mvc.ViewUserControl viewUserControl)
        {
            foreach (var property in viewUserControl.GetType().GetProperties())
            {
                if (viewUserControl.ViewData[property.Name] != null)
                    property.SetValue(viewUserControl, Convert.ChangeType(viewUserControl.ViewData[property.Name], property.PropertyType), null);
            }
        }

    }
}

Hope that’s useful to you!


Adding a prompt option to Html.Select in ASP.NET MVC

Digging around the source code to the ASP.NET MVC Source Refresh, I discovered a useful extension to the Html.Select helper method.

Say you want a select dropdown with a NULL case, which instructs the user to make a selection.

Eg: You want your first option to be:  ==Select==

To do so, call Html.Select with a htmlAttribute of prompt=”==Select==”

Like:

<%=Html.Select((“Quote.ClientId”), ViewData.Clients, “Name”,”Id”, ViewData.Quote.ClientId,0,false,new {prompt=”==Select==”})%>