我有一个问题,我在这个地方描述了如何创建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)