2012-06-25 45 views
0

我有一个问题,我形容它在这个地方How create UIHit YesNoNull?是否可以通过属性来做同样的事情?

我通过创建一个助手解决了这个问题:

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()); 
} 

我用它这样:

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

是有可能做同样的事情,但没有使用帮手和唯一的设计:

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

回答

1

我相信你可以在~/Views/Shared/EditorTemplates/中创建一个名为YesNoNull.cshtml的模板,并在其中添加标记。

每当您在视图中调用@Html.EditorFor时,它会在~/Views/Shared/EditorTemplates~/Views/Shared/DisplayTemplates/中查找与模型类型相匹配的模板。

因此,对于字符串而言,需要String.cshtml,对于DateTime,需要使用DateTime.cshtml,依此类推。如果你定义一个UIHint属性,它应该把你所谓的UIHint作为模板

+0

而且不知怎的,我做不到,因为我需要 – Mediator

相关问题