0

형식에 따라 동적으로보기에서 컨트롤을 만들려는 경우 = 텍스트 상자 확인란이 선택되어 있으면 텍스트 상자 만들기 확인란을 선택하면 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; } 
} 
+1

왜 조건 if''사용하지 코드? –

+0

당신은 할 수있는보기 (cshtml) 페이지에서 의미하지만 다른 모양을 줄 필요도 샘플 HTML을 참조하십시오 – Rutu

+0

나는 [링크] http://stackoverflow.com/questions/6329461/how- to-create-controls-dynamic-in-mvc-3-based-on-an-xml-file 작동하지만 괜찮습니다. 즉, 제출 단추를 클릭하면 viewmodel이 null입니다. 게시 된 코드의 – Rutu

답변

0

운 좋은

public class ControlModelBinder : DefaultModelBinder 
    { 
    protected override object CreateModel(ControllerContext controllerContext,    ModelBindingContext bindingContext, Type modelType) 
    { 
     var datalist = controllerContext.HttpContext.Request.Form.GetEnumerator(); 
     SurveyViewModel model = new SurveyViewModel(); 
     model.ControlsList = new List<ControlViewModel>(); 
     List<string> answers = new List<string>(); 
     while (datalist.MoveNext()) 
     { 
      string currentKey = datalist.Current.ToString(); 
      if (currentKey.Contains("TextBoxViewModel")) 
      { 
       TextBoxViewModel textBoxViewModel = new TextBoxViewModel(); 
       textBoxViewModel.Value = controllerContext.HttpContext.Request.Form[currentKey]; 
       model.ControlsList.Add(textBoxViewModel); 
      } 
      else if (currentKey.Contains("CheckBoxViewModel")) 
      { 
       CheckBoxViewModel checkboxviewmodel = new CheckBoxViewModel(); 
       checkboxviewmodel.SelectedValues = controllerContext.HttpContext.Request.Form[currentKey]; 
       model.ControlsList.Add(checkboxviewmodel); 
      } 
      else if (currentKey.Contains("RadioBoxViewModel")) 
      { 
       RadioBoxViewModel radioboxviewmodel = new RadioBoxViewModel(); 
       radioboxviewmodel.SelectedValue = controllerContext.HttpContext.Request.Form[currentKey]; 
       model.ControlsList.Add(radioboxviewmodel); 
      } 
      else if (currentKey.Contains("RattingBoxViewModel")) 
      { 
       RattingBoxViewModel rattingboxviewmodel = new RattingBoxViewModel(); 
       rattingboxviewmodel.Score = controllerContext.HttpContext.Request.Form[currentKey]; 
       model.ControlsList.Add(rattingboxviewmodel); 
      } 
      else if (currentKey.Contains("SliderViewModel")) 
      { 
       SliderViewModel sliderviewmodel = new SliderViewModel(); 
       sliderviewmodel.Value = controllerContext.HttpContext.Request.Form[currentKey]; 
       model.ControlsList.Add(sliderviewmodel); 
      } 
     } 
     return model; 
    } 

} 
0

있는 조건이 추가 문제가 발생할하지 않을 경우 너를 위해서. 단지보기에 표시 할 필드를 변경합니다. 유효성 검사 및 모델에 대한 연결은 변경되지 않습니다.

@if(condition1){ 
<h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry? </h2> 
    <ul> 
     <li> 
      @Html.RadioButtonFor(x => x.r1, "1") 
      <label for="r1">Single choice option 1</label> 
     </li> 
     <li> 
      @Html.RadioButtonFor(x => x.r2, "2") 
      <label for="r2">Single choice option 2</label> 
     </li> 
     <li> 
      @Html.RadioButtonFor(x => x.r3, "3") 
      <label for="r3">Single choice option 3</label> 
     </li> 
     <li> 
      @Html.RadioButtonFor(x => x.r4, "4") 
      <label for="r4">Single choice option 4</label> 
     </li> 
    </ul> 
    }else if(condition2){ 
    <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry?</h2> 
    <ul> 
     <li> 
      @Html.CheckBoxFor(x => x.cb10) 
      <label for="cb10">Multiple choice option 1</label> 
     </li> 
     <li> 
      @Html.CheckBoxFor(x => x.cb11) 
      <label for="cb11">Multiple choice option 2</label> 
     </li> 
     <li> 
      @Html.CheckBoxFor(x => x.cb12) 
      <label for="cb12">Multiple choice option 3</label> 
     </li> 
     <li> 
      @Html.CheckBoxFor(x => x.cb13) 
      <label for="cb13">Multiple choice option 4</label> 
    </li> 
</ul> 
} 

따라서 조건 1이 충족되면 라디오 단추 만 양식에 렌더링됩니다. 조건이 충족되는 경우 박스

을 확인

업데이트 :이 훨씬 더 어려운 모델에 넥타이를하는 것 변경된 코드를 기반으로

. 난 당신이 동적으로 생성되는 필드에 이름을 넣고이 확인 된 아이디의 쉼표 묘사 목록을 반환합니다 예를 들어 체크 박스리스트 (1,2,3위한

Request.Form["fieldName"].ToString() 

을하는 것이 좋습니다 것입니다 기타). 이봐, 난 여기에 사용자 정의 모델 바인더 를 만들어이 문제를 해결

+0

감사합니다. Matt 위의 코드를 올바르게 업데이트 해 주시길 바랍니다. 그러나보기가 tighlty 모델에 바인딩되어 있지 않습니다. 나는 서면 커스텀 모델로 문제를 직면하고 있습니다. 접합재. – Rutu

+0

위의 내 업데이트를 참조하십시오. –

+0

에 대한 내 맞춤 데이터 바인더를 만들 수 있습니다.이 부분은 – Rutu