Mediator
Mediator

Reputation: 15378

is it possible to do the same through the attributes?

I have a problem, I described it in this place How create UIHit YesNoNull?

I solved this problem by creating a helper:

public static MvcHtmlString YesNoNull(this HtmlHelper helper, string name)
{
    return YesNoNull(helper, name, false);
}

public static MvcHtmlString YesNoNull(this HtmlHelper helper, string name, bool selected)
{
    var builder = new StringBuilder(500);
    builder.Append(string.Format("<input id='checkbox_{0}' type='checkbox' />Есть", name));
    builder.Append("<script type=\"text/javascript\">$(function () {");
    builder.Append(string.Format("$('#checkbox_{0}').click(function () {{", name));
    builder.Append(@"var checked = $(this).attr('checked');if (checked != undefined) {");
    builder.Append(string.Format("$('#{0}').show();}}", name));
    builder.Append(string.Format("else {{$('#{0}').hide();}}  }});", name));
    builder.Append(string.Format("$('#checkbox_{0}').attr('checked', {1});", name, selected.ToString().ToLower())); 
    builder.Append(string.Format("var checked = $('#checkbox_{0}').attr('checked');if (checked != undefined){{ $('#{0}').show(); }}else{{$('#{0}').hide();}}", name));
    builder.Append("})</script>");




    return new MvcHtmlString(builder.ToString());
}

I use it so:

@Html.YesNoNull("VenueUrl", false)
@Html.EditorFor(model => model.VenueUrl)

is it possible to do the same thing but without the use of the helper and the only one design:

@Html.EditorFor(model => model.VenueUrl)

Upvotes: 0

Views: 68

Answers (1)

mfussenegger
mfussenegger

Reputation: 3971

I believe you could create a template in ~/Views/Shared/EditorTemplates/ called YesNoNull.cshtml and add the markup there.

Whenever you call @Html.EditorForin your views, it looks in ~/Views/Shared/EditorTemplates or ~/Views/Shared/DisplayTemplates/ for a template that matches the models type.

So, for strings it would take String.cshtml, for DateTime it would take DateTime.cshtml, and so on. If you define a UIHint attribute it should take whatever you called the UIHint as template

Upvotes: 1

Related Questions