我想動態地根據源創建控件,如果type = text box創建文本框如果是複選框在MVC中動態創建複選框。下面是我當前的代碼在mvc中動態創建控件
@model PayTxn.Miscellaneous.Models.SurveyViewModel
@using PayTxn.Miscellaneous.Models
@{ int index = 0;}
@for (int i = 0; i < Model.ControlsList.Length; i++)
{
var control = Model.ControlsList[i];
if (control.Type == "radio")
{
Html.RenderPartial("~/Views/Shared/EditorTemplates/_RadioBoxViewModel.cshtml", control as RadioBoxViewModel, new ViewDataDictionary { { "index", index } });
}
else if (control.Type == "checkbox")
{
Html.RenderPartial("~/Views/Shared/EditorTemplates/_CheckBoxViewModel.cshtml", control as CheckBoxViewModel, new ViewDataDictionary { { "index", index } });
}
else if (control.Type == "textbox")
{
Html.RenderPartial("~/Views/Shared/EditorTemplates/_TextBoxViewModel.cshtml", control as TextBoxViewModel, new ViewDataDictionary { { "index", index } });
}
else if (control.Type == "rattingbox")
{
Html.RenderPartial("~/Views/Shared/EditorTemplates/_RattingBoxViewModel.cshtml", control as RattingBoxViewModel, new ViewDataDictionary { { "index", index } });
}
else if (control.Type == "slider")
{
Html.RenderPartial("~/Views/Shared/EditorTemplates/_SliderViewModel.cshtml", control as SliderViewModel, new ViewDataDictionary { { "index", index } });
}
index = index + 1;
}
<input type="submit" name="action:Submit1" value="Submit1" />
<input type="submit" name="action:Reset" value="Reset" />
它工作正常,但在submit1按鈕的點擊我的看法是沒有緊密結合模型 型號代碼
public class SurveyViewModel
{
//public List<ControlViewModel> ControlsList { get; set;
public ControlViewModel[] ControlsList { get; set; }
}
public abstract class ControlViewModel
{
public abstract string Type { get; }
public bool Visible { get; set; }
public string Label { get; set; }
public string Name { get; set; }
}
public class TextBoxViewModel : ControlViewModel
{
public override string Type
{
get { return "textbox"; }
}
public string Value { get; set; }
}
public class RadioBoxViewModel : ControlViewModel
{
public List<string> Options { get; set; }
public List<string> Values { get; set; }
public override string Type
{
get { return "radio"; }
}
}
public class CheckBoxViewModel : ControlViewModel
{
public List<string> Options { get; set; }
public List<string> Values { get; set; }
public override string Type
{
get { return "checkbox"; }
}
public bool Value { get; set; }
}
public class SliderViewModel : ControlViewModel
{
public override string Type
{
get { return "slider"; }
}
public string Value { get; set; }
}
public class RattingBoxViewModel : ControlViewModel
{
public List<string> Titles { get; set; }
public List<string> Rattings { get; set; }
public string _rattingType = null;
public string RattingType
{
get
{
if (string.IsNullOrEmpty(_rattingType))
return "star";
else
return _rattingType;
}
set
{
_rattingType = value;
}
}
public override string Type
{
get { return "rattingbox"; }
}
public bool Value { get; set; }
}
爲什麼不使用'if'條件? –
你的意思是在視圖(cshtml)頁面,可以完成,但我需要給它不同的外觀感覺也請參閱示例html – Rutu
我試過使用[鏈接] http://stackoverflow.com/questions/6329461/how-在創建控制動態在mvc-3基於一個xml文件它工作正常,但它沒有嚴格限制,即當我點擊提交按鈕viewmodel爲null – Rutu