我正在使用 C#、MVC4 和 jQuery。给我的要求是我需要能够将记录添加到数据库中,该数据库可以转换为视图中的可编辑字段。
该视图具有选项卡、组、行和项,需要根据数据库中的内容进行填充。我已经能够使用包含“选项卡”列表的视图模型来实现我想要的;每个选项卡都包含一个“组”列表;每个组包含一个“行”列表,每个行包含一个“项目”列表。
对我来说最终发生的事情是我需要重复很多对编辑器完全相同的代码。这是我想要避免的。
所以在我看来,我有类似的东西:
@for (int tabIndex = 0; Model.Tabs != null && tabIndex < Model.Tabs.Count; tabIndex++)
{
<div id="tab-@Model.Tabs[tabIndex].TabID" class="tab-content">
@for (int groupIndex = 0; Model.Tabs[tabIndex].Groups != null && groupIndex < Model.Tabs[tabIndex].Groups.Count; groupIndex++)
{
<legend>@Model.Tabs[tabIndex].Groups[groupIndex].Name</legend>
//This is the part that is not working but I would like to
AddEditorsForGroup(tabIndex, groupIndex);
...
...
然后我也有这个观点:
@{
public void AddEditorsForGroup(int TabIndex, int GroupIndex)
{
for (int lineIndex = 0; Model.Tabs[TabIndex].Groups[GroupIndex].Lines != null && lineIndex < Model.Tabs[TabIndex].Groups[GroupIndex].Lines.Count; lineIndex++)
{
for (int itemIndex = 0; Model.Tabs[TabIndex].Groups[GroupIndex].Lines[lineIndex].Items != null && itemIndex < Model.Tabs[TabIndex].Groups[GroupIndex].Lines[lineIndex].Items.Count; itemIndex++)
{
AddLabelAndEditor(TabIndex, GroupIndex, lineIndex, itemIndex);
}
}
}
public void AddLabelAndEditor(int TabIndex, int GroupIndex, int LineIndex, int ItemIndex)
{
<div class="_20">
<p>
switch (Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].DisplayTypeEdit.ToLower().Replace(" ", string.Empty))
{
case "checkbox":
Html.LabelFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueBoolean, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].Name);
Html.CheckBoxFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueBoolean);
break;
case "dropdownlist":
Html.LabelFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].Name);
Html.DropDownListFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueSelectList);
break;
case "multiselectlist":
Html.LabelFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].Name);
Html.DropDownListFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueMultiSelectList);
break;
case "radiobutton":
Html.RadioButtonFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString);
break;
case "textarea":
Html.LabelFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].Name);
Html.TextAreaFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString);
break;
default:
Html.LabelFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString, Model.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].Name);
Html.TextBoxFor(vm => vm.Tabs[TabIndex].Groups[GroupIndex].Lines[LineIndex].Items[ItemIndex].ValueString);
break;
}
</p>
</div>
}
}
如果我AddLabelAndEditor
在视图的几个部分重复包含的代码,它可以工作,但它是很多代码重复。
我的 ViewModel 的相关部分:
public List<Tab> Tabs { get; set; }
public class Tab
{
public string Name { get; set; }
public List<Group> Groups { get; set; }
}
public class Group
{
public string Name { get; set; }
public List<Line> Lines { get; set; }
}
public class Line
{
public string Name { get; set; }
public List<Item> Items { get; set; }
}
public class Item
{
public string Name { get; set; }
public string Description { get; set; }
public string DataType { get; set; }
public string DataDefaultValue { get; set; }
public string DisplayTypeEdit { get; set; }
public string ValueString { get; set; }
public bool ValueBoolean { get; set; }
public DateTime ValeDateTime { get; set; }
public decimal ValueDecimal { get; set; }
public int ValueInt { get; set; }
public MultiSelectList ValueMultiSelectList { get; set; }
public SelectList ValueSelectList { get; set; }
}
有什么想法、想法、建议吗?谢谢