I'm creating a helper that will allow me to create cascading drop down lists that fill themselves using AJAX. The helper method looks like this :
public static MvcHtmlString AjaxSelectFor<TModel, TProperty>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TProperty>> expression,
Expression<Func<TModel, TProperty>> cascadeFrom,
string sourceUrl,
bool withEmpty = false)
{
string controlFullName = html.GetControlName(expression);
string cascadeFromFullName = html.GetControlName(cascadeFrom);
var selectBuilder = GetBaseSelect(controlFullName.GetControlId(), controlFullName, sourceUrl, withEmpty);
selectBuilder.Attributes.Add("data-selected-id", html.GetValue(expression));
selectBuilder.Attributes.Add("data-cascade-from", "#" + cascadeFromFullName.GetControlId());
return new MvcHtmlString(selectBuilder.ToString());
}
private static TagBuilder GetBaseSelect(string controlId, string controlName, string sourceUrl, bool withEmpty)
{
var selectBuilder = new TagBuilder("select");
selectBuilder.Attributes.Add("id", controlId);
selectBuilder.Attributes.Add("name", controlName);
selectBuilder.Attributes.Add("data-toggle", "ajaxSelect");
selectBuilder.Attributes.Add("data-source-url", sourceUrl);
selectBuilder.Attributes.Add("data-with-empty", withEmpty.ToString());
selectBuilder.AddCssClass("form-control");
return selectBuilder;
}
internal static string GetControlName<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
{
string controlName = ExpressionHelper.GetExpressionText(expression);
return html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(controlName);
}
internal static string GetControlId(this string controlName)
{
return TagBuilder.CreateSanitizedId(controlName);
}
The first expression targets the property that will be bound in the control and I have no problem getting the id and name attributes for it. The second targets the property that the helper will cascade from, but when I get through the GetControlName method, ExpressionHelper.GetExpressionText(expression) returns an empty string instead of the property name. I added a watch on "expression" to check what was going wrong, and its value is as follows :
{model => Convert(model.TopCategoryId)}
While I get the following value when I'm getting the property name for the first expression :
{model => model.CategoryId}
I really don't understand why there is a difference between the two expressions. Here's how I call the helper on my view, in case it's relevant anyhow :
@Html.AjaxSelectFor(model => model.CategoryId, model => model.TopCategoryId, "/api/Categories/GetSelectList", true)
Any idea what's going on here ?
After having used a hacky workaround for some time, I finally figured it out. As Stephen Muecke pointed out, the problem came from using the type TProperty for both "expression" and "cascadeFrom". So, here's how to properly (well, kind of) solve this problem :
Hope that might help someone !
[Edit]
By the way, here's the jQuery code to make this work :