/// <summary> /// Generate an HTML input element of type 'checkbox' and set checked from ViewData based on the name provided. /// The checkbox element has an accompanying input element of type 'hidden' to support binding upon form post. /// </summary> /// <param name="view">The view.</param> /// <param name="name">Value of the 'name' attribute of the element. Also used to derive the 'id' attribute.</param> public static CheckBox CheckBox(this IViewDataContainer view, string name) { var checkbox = new CheckBox(name).Value(true); var chkd = view.ViewData.Eval(name) as bool?; if (chkd.HasValue) { checkbox.Checked(chkd.Value); } return checkbox; }
/// <summary> /// Generate an HTML input element of type 'checkbox' and set its value from the ViewModel based on the expression provided. /// The checkbox element has an accompanying input element of type 'hidden' to support binding upon form post. /// </summary> /// <typeparam name="T">The type of the ViewModel.</typeparam> /// <param name="view">The view.</param> /// <param name="expression">Expression indicating the ViewModel member associated with the element.</param> public static CheckBox CheckBox <T>(this IViewModelContainer <T> view, Expression <Func <T, object> > expression) where T : class { var checkbox = new CheckBox(expression.GetNameFor(view), expression.GetMemberExpression(), view.Behaviors); var val = expression.GetValueFrom(view.ViewModel) as bool?; if (val.HasValue) { checkbox.Checked(val.Value); } return(checkbox); }
private void VisualCheckBoxCheck(CheckBox a_checkbox) { //if inside a button if (a_checkbox.IsMouseOver(m_input.m_mouse.m_mouseState.X, m_input.m_mouse.m_mouseState.Y)) { if (m_input.mouseLeftClick()) { if (a_checkbox.m_state == CheckBox.ChkState.Unchecked) { //check box a_checkbox.Checked(); } else if (a_checkbox.m_state == CheckBox.ChkState.Checked) { //uncheck box a_checkbox.Unchecked(); } } } }
/// <summary> /// Визуализация свойства модели для редактирования. /// </summary> /// <typeparam name="T">Класс объекта модели.</typeparam> /// <param name="entity">Объект модели.</param> /// <param name="html">Объект для визуализации HTML контролов.</param> /// <param name="expr">Выражение для получения значения свойства.</param> /// <param name="defaultValue">Значение по умолчанию, если объект модели равен null.</param> public static string RenderEditable <T>(this Entity entity, HtmlHelper html, Expression <Func <T, object> > expr, object defaultValue) where T : Entity { var pi = new PropertyInfo <T, object>((T)entity, expr, defaultValue); StringBuilder sb = new StringBuilder(); sb.Append("<div class=\"property\">"); // Чекбокс if (pi.ReturnType == typeof(bool)) { CheckBox checkBox = new CheckBox(pi.TagName); checkBox.Id(pi.TagId); checkBox.Checked(pi.Value.Equals(String.Empty) ? false : Convert.ToBoolean(pi.Value)); sb.Append("<span class=\"property-value\">"); sb.Append(checkBox); sb.Append("</span>"); sb.AppendFormat("<label class=\"property-label\" for=\"{0}\">{1}</label>", pi.TagId, pi.Caption); sb.Append(html.ValidationMessage(pi.TagName)); } else { sb.AppendFormat("<label class=\"property-label\" for=\"{0}\">{1}</label>", pi.TagId, pi.Caption); sb.Append(html.ValidationMessage(pi.TagName)); sb.Append("<div class=\"property-value\">"); if (pi.ReturnType.BaseType == typeof(Entity)) { } // Вылетающий список для перечислений else if (pi.ReturnType.BaseType == typeof(Enum)) { Dictionary <int, string> list = new Dictionary <int, string>(); foreach (var item in Enum.GetValues(pi.ReturnType)) { list.Add(Convert.ToInt32(item), GetEnumItemDescription(pi.ReturnType, item)); } Select select = new Select(pi.TagName); select.Id(pi.TagId); select.Options(list); select.Selected(pi.Value); sb.Append(select); } // Область ввтода многострочная else if (pi.TextArea != null) { TextArea textArea = new TextArea(pi.TagName); textArea.Columns(pi.TextArea.Cols); textArea.Rows(pi.TextArea.Rows); textArea.Id(pi.TagId); textArea.Value(pi.Value); sb.Append(textArea); sb.Append(BBCodeNote); } // Поле ввода однострочное else { TextBox textBox = new TextBox(pi.TagName); textBox.Id(pi.TagId); textBox.Value(pi.Value); sb.Append(textBox); } sb.Append("</div>"); } // Описание поля ввода if (!String.IsNullOrEmpty(pi.Description)) { sb.AppendFormat("<div class=\"note\">{0}</div>", pi.Description); } sb.Append("</div>"); return(sb.ToString()); }