引入列表视图模型,简化视图
This commit is contained in:
parent
0205423945
commit
66eb230031
|
@ -9,7 +9,7 @@
|
|||
<label for="roleId" class="control-label mb-0">角色:</label>
|
||||
@*@Html.ForDropDownList("roleId", Role.FindAllWithCache(), page["roleId"], "全部", true)*@
|
||||
@*@Html.ForListBox("roleIds", Role.FindAllWithCache(), page["roleIds"])*@
|
||||
@await Html.PartialAsync("_Form_ListBox", new ItemModel("roleIds", Role.FindAllWithCache(), null, page["roleIds"], null))
|
||||
@await Html.PartialAsync("_Form_ListBox", new ListBoxModel("roleIds", Role.FindAllWithCache(), page["roleIds"]))
|
||||
</div>
|
||||
@await Html.PartialAsync("_List_Toolbar_Enable")
|
||||
@*@await Html.PartialAsync("_List_Toolbar_SelectDepartment", "departmentId")*@
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
@if (dataSource != null)
|
||||
{
|
||||
@*@Html.ForListBox(item.Name, dataSource(entity, item), entity[item.Name] as String)*@
|
||||
@await Html.PartialAsync("_Form_ListBox", new ItemModel(item.Name, dataSource(entity, item), null, entity[item.Name] as String, null))
|
||||
@await Html.PartialAsync("_Form_ListBox", new ListBoxModel(item.Name, dataSource(entity, item), entity[item.Name]))
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<label for="roleId" class="control-label">角色:</label>
|
||||
@*@Html.ForDropDownList("roleId", Role.FindAllWithCache(), page["roleId"], "全部", true)*@
|
||||
@*@Html.ForListBox("roleIds", Role.FindAllWithCache(), page["roleIds"])*@
|
||||
@await Html.PartialAsync("_Form_ListBox", new ItemModel("roleIds", Role.FindAllWithCache(), null, page["roleIds"], null))
|
||||
@await Html.PartialAsync("_Form_ListBox", new ListBoxModel("roleIds", Role.FindAllWithCache(), page["roleIds"]))
|
||||
</div>
|
||||
@await Html.PartialAsync("_Enable")
|
||||
@await Html.PartialAsync("_SelectDepartment", "departmentId")
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace NewLife.Cube.ViewModels
|
||||
{
|
||||
/// <summary>下拉列表模型</summary>
|
||||
public class ListBoxModel
|
||||
{
|
||||
#region 属性
|
||||
/// <summary>名称</summary>
|
||||
public String Name { get; set; }
|
||||
|
||||
/// <summary>数据</summary>
|
||||
public IEnumerable Value { get; set; }
|
||||
|
||||
/// <summary>已选值</summary>
|
||||
public Object SelectedValues { get; set; }
|
||||
|
||||
/// <summary>标签</summary>
|
||||
public String OptionLabel { get; set; }
|
||||
|
||||
/// <summary>自动提交</summary>
|
||||
public Boolean AutoPostback { get; set; }
|
||||
|
||||
/// <summary>Html特性</summary>
|
||||
public Object HtmlAttributes { get; set; }
|
||||
#endregion
|
||||
|
||||
#region 构造
|
||||
/// <summary>实例化</summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="selectedValues"></param>
|
||||
public ListBoxModel(String name, IEnumerable value, Object selectedValues)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
SelectedValues = selectedValues;
|
||||
}
|
||||
|
||||
/// <summary>实例化</summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="selectedValues"></param>
|
||||
/// <param name="optionLabel"></param>
|
||||
/// <param name="autoPostback"></param>
|
||||
public ListBoxModel(String name, IEnumerable value, Object selectedValues, String optionLabel, Boolean autoPostback, Object htmlAttributes)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
SelectedValues = selectedValues;
|
||||
OptionLabel = optionLabel;
|
||||
AutoPostback = autoPostback;
|
||||
HtmlAttributes = htmlAttributes;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -12,5 +12,5 @@
|
|||
<div class="form-group">
|
||||
<label for="enable" class="control-label">状态:</label>
|
||||
@*@Html.ForDropDownList("enable", dic, page["enable"], "全部", true)*@
|
||||
@await Html.PartialAsync("_Form_DropDownList", new ItemModel("enable", dic, null, page["enable"], new{ optionLabel = "全部", autoPostback = true }))
|
||||
@await Html.PartialAsync("_Form_DropDownList", new ListBoxModel("enable", dic, page["enable"], "全部", true, null))
|
||||
</div>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
@model ItemModel
|
||||
@model ListBoxModel
|
||||
@using NewLife;
|
||||
@using NewLife.Cube
|
||||
@using XCode;
|
||||
|
@ -7,7 +7,7 @@
|
|||
@{
|
||||
var name = Model.Name;
|
||||
var value = Model.Value;
|
||||
var type = Model.Type ?? value.GetType();
|
||||
var type = value.GetType();
|
||||
|
||||
var atts = HtmlHelper.AnonymousObjectToHtmlAttributes(Model.HtmlAttributes);
|
||||
if (NewLife.Cube.Setting.Current.BootstrapSelect)
|
||||
|
@ -16,18 +16,21 @@
|
|||
atts.Add("class", "form-control");
|
||||
|
||||
// 处理自动回发
|
||||
var autoPostback = atts.TryGetValue("autoPostback", out var val) && val.ToBoolean();
|
||||
if (autoPostback) atts.Add("onchange", "$(this).parents('form').submit();");
|
||||
if (Model.AutoPostback) atts.Add("onchange", "$(this).parents('form').submit();");
|
||||
|
||||
var optionLabel = atts.TryGetValue("optionLabel", out var val2) ? (val2 + "") : "";
|
||||
var optionLabel = Model.OptionLabel;
|
||||
|
||||
var selectedValue = Model.Format;
|
||||
var selectedValue = Model.SelectedValues;
|
||||
}
|
||||
@if (type.As(typeof(IEnumerable)) && type.GetElementTypeEx().As(typeof(IEntity)))
|
||||
@if (value is IEnumerable<SelectListItem> selectList)
|
||||
{
|
||||
@Html.DropDownList(name, selectList, atts)
|
||||
}
|
||||
else if (type is IEnumerable data && type.GetElementTypeEx().As(typeof(IEntity)))
|
||||
{
|
||||
IEntityFactory factory = null;
|
||||
var dic = new Dictionary<String, String>();
|
||||
foreach (IEntity entity in value as IEnumerable)
|
||||
foreach (IEntity entity in data)
|
||||
{
|
||||
if (factory == null) factory = entity.GetType().AsFactory();
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
@if (dataSource != null)
|
||||
{
|
||||
@*@Html.ForListBox(item.Name, dataSource(entity, item), entity[item.Name] as String)*@
|
||||
@await Html.PartialAsync("_Form_ListBox", new ItemModel(item.Name, dataSource(entity, item), null, entity[item.Name] as String, null))
|
||||
@await Html.PartialAsync("_Form_ListBox", new ListBoxModel(item.Name, dataSource(entity, item), entity[item.Name]))
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
@model ItemModel
|
||||
@model ListBoxModel
|
||||
@using NewLife;
|
||||
@using NewLife.Cube
|
||||
@using XCode;
|
||||
|
@ -7,7 +7,7 @@
|
|||
@{
|
||||
var name = Model.Name;
|
||||
var value = Model.Value;
|
||||
var type = Model.Type ?? value.GetType();
|
||||
var type = value.GetType();
|
||||
|
||||
var atts = HtmlHelper.AnonymousObjectToHtmlAttributes(Model.HtmlAttributes);
|
||||
if (NewLife.Cube.Setting.Current.BootstrapSelect)
|
||||
|
@ -16,16 +16,20 @@
|
|||
atts.Add("class", "form-control");
|
||||
atts.Add("multiple", "");
|
||||
|
||||
var selectedValues = Model.Format;
|
||||
var selectedValues = Model.SelectedValues as String;
|
||||
var selectedList = selectedValues?.Split(",");
|
||||
}
|
||||
@if (type.As(typeof(IEnumerable)) && type.GetElementTypeEx().As(typeof(IEntity)))
|
||||
@if (value is IEnumerable<SelectListItem> selectList)
|
||||
{
|
||||
@Html.ListBox(name, selectList, atts)
|
||||
}
|
||||
else if (type is IEnumerable data && type.GetElementTypeEx().As(typeof(IEntity)))
|
||||
{
|
||||
//var vs = EntityExtension.ToDictionary(value);
|
||||
//var vs = typeof(EntityExtension).Invoke("ToDictionary", value);
|
||||
IEntityFactory factory = null;
|
||||
var dic = new Dictionary<String, String>();
|
||||
foreach (IEntity entity in value as IEnumerable)
|
||||
foreach (IEntity entity in data)
|
||||
{
|
||||
if (factory == null) factory = entity.GetType().AsFactory();
|
||||
|
||||
|
|
|
@ -49,6 +49,6 @@
|
|||
var dic3 = dic.ToDictionary(e => e.Key + "", e => e.Value);
|
||||
|
||||
//@Html.ForDropDownList(field.MapField, dic, entity[field.MapField])
|
||||
@await Html.PartialAsync("_Form_DropDownList", new ItemModel(field.MapField, dic3, null, entity[field.MapField] + "", null))
|
||||
@await Html.PartialAsync("_Form_DropDownList", new ListBoxModel(field.MapField, dic3, entity[field.MapField] + ""))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue