형식에 따라 동적으로보기에서 컨트롤을 만들려는 경우 = 텍스트 상자 확인란이 선택되어 있으면 텍스트 상자 만들기 확인란을 선택하면 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- to-create-controls-dynamic-in-mvc-3-based-on-an-xml-file 작동하지만 괜찮습니다. 즉, 제출 단추를 클릭하면 viewmodel이 null입니다. 게시 된 코드의 – Rutu