I'm working on ASP.NE MVC4 and I'm using LungoJS library for the development.
I'm developing a Helper to render LungoJS controls in the way I need.
For the Select inputs, I want to get a JSON that contains the data for each option.
I can get the needed JSON from an Action of a existing Controller.
But when I call the action from the helper, the view renders my html inside a <pre>
tag :-(
View
<div class="form" id="address-data">
@Html.LungoInputFor(m => m.CaseServiceCaseModel.CaseAddressModel.AddressDataModel.TerritoryId, "text", @Labels.Territory, @htmlAttributesRO, true)
@Html.LungoSelectFor(m => m.CaseServiceCaseModel.CaseAddressModel.AddressDataModel.TerritoryId, @Labels.Territory, null, true, "GetTerritoryKendo", "Address")
Helper
public static MvcHtmlString LungoSelectFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string label = "", object htmlAttributes = null, bool fieldset = false, string action = "", string controller = "")
{
TagBuilder tag = new TagBuilder("select");
JavaScriptSerializer js = new JavaScriptSerializer();
MvcHtmlString data = ChildActionExtensions.Action(html, action, controller);
object[] json = js.Deserialize<object[]>(data.ToString());
foreach (Dictionary<string,object> item in json)
{
foreach (var property in item)
{
TagBuilder option = new TagBuilder("option");
switch (property.Key)
{
case "Selected":
if ((bool)property.Value)
{
option.MergeAttribute("selected","selected");
}
break;
case "Text":
option.InnerHtml = property.Value.ToString();
break;
case "Value":
option.MergeAttribute("value", property.Value.ToString());
break;
default:
break;
}
tag.InnerHtml += option.ToString(TagRenderMode.Normal);
}
}
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
I don't understand why if I'm getting the htmlstring from the Action in a variable, and after I return only the Select input html, affects the whole page... If I comment ChildActionExtensions.Action(html, action, controller)
the view renders good (but without the content I need...)
You should not be using child actions here (
ChildActionExtensions.Action
helper) because they write directly to the response. What you could do instead is to include a property on your view model that will contain the collection you need and directly pass it as argument to your helper. Alternatively (but this is not a good solution) would be to call your DAL in the helper to retrieve the list.