2015-05-13 32 views
0

我想要生成動態表單控件,其中表單變量及其各自的數據類型存儲在MySql數據庫表中。這是我用來在窗體上渲染主變量的代碼。但我嘗試過的選項有一些或其他問題。該代碼都爲:在MVC中呈現動態表單控件4

Labels.cs

public partial class Labels 
{ 
    public int Label_Id { get; set; } 
    public string Label_Name { get; set; } 
    public string Form_Element_Type { get; set; } 
} 

public partial class Observation 
{ 
    public int Observation_Id {get; set;} 
    public int Employee_Id {get; set;} 
    public int Label_Id {get; set;} 
    public string Value {get; set;} 
} 

模型類

public class EmployeeModel 
{ 
    public Dictionary<string, string> ObsDictionay { get; set; } 
} 

控制器

Public class EmployeeController 
{ 
    Public ActionResult ProcessEmployee() 
{ 
     List<MasterLabel> masterLabels = new List<MasterLabel>(); 
       IMasterLabel masterLabelManager = new MasterLabelsManager(); 
       masterLabels = masterLabelManager.GetAll(); 
       model.MasterLabels = masterLabels; 

    IObservation obsManager = new ObservationManager(); 
       List<Observation> obsList = new List<Observation>(); 
       obsList = obsManager.GetAllByPatientId(id); 
       Dictionary<string, string> ObsDictionay = new Dictionary<string, string>(); 

       foreach (var label in masterLabels) 
       { 
        int flag = 0; 
        if (obsList.Count() > 0) 
        { 
         foreach (var obs in obsList) 
         { 
          if (label.Label_Id == obs.Label_Id) 
          { 
           ObsDictionay.Add(label.Label_Id.ToString(), obs.Value); 
           flag = 1; 
           break; 
          } 

         } 
         if (flag == 0) 
         { 
          ObsDictionay.Add(label.Master_Label_Id.ToString(), null); 
         } 

        } 
        else 
        { 
         ObsDictionay.Add(label.Master_Label_Id.ToString(), null); 
        } 

       } 

       model.ObsDictionay = ObsDictionay; 


} 

} 

,我已經嘗試過,並沒有履行查看---選項要求

OPTION 1:

<input class="control-label" type="@item.Form_Element_Type" id="@item.Master_Label_Id" name="@item.Master_Label_Id " value= @Model.ObsDictionay[item.Label_Id.ToString()]"> 

此選項重新調諧空ObsDictionay爲[HttpPost]方法在控制器

選項2:

@Html.EditorFor(x => x.ObsDictionay[item.Label_Id.ToString()]) 

此選項重新調諧ObsDictionay爲[ HttpPost]方法在控制器中的所有值,但爲所有變量呈現文本框LES

OPTION 3:

@if (item.Form_Element_Type == "checkbox") 
{ 
    @Html.CheckBoxFor(x => x.ObsDictionay[item.Label_Id.ToString()] == "Y"? true : false) 
} 
else if(item.Form_Element_Type == "textbox") 
{ 
@Html.TextAreaFor(x => x.ObsDictionay[item.Label_Id.ToString()])           
} 

ERROR信息:System.InvalidOperationException:模板只能與現場訪問,屬性訪問,一維數組的索引,或者單參數定製索引表達式中使用,同時呈現複選框。

有人可以幫助我。

+0

我在其中一個項目中使用了動態表單,並且還編寫了[blog](http://blogs.quovantis.com/dynamic-models-for-mvc-view/)。我發現Expando對象和動態類型視圖更合適 – Anjani

+0

選項1不起作用,因爲模型綁定是通過屬性名稱完成的。而不是像「@ item.Master_Label_Id」這樣的名稱嘗試給出控制名稱,如「@ item.ObsDictionay [item.Label_Id.ToString()]」 – Anjani

回答

0

選項1

它應該是<input class="control-label" type="@item.Form_Element_Type" id="@item.Master_Label_Id" name="@item.Master_Label_Name " value= @Model.ObsDictionay[item.Label_Id.ToString()]">

我不知道爲什麼你有@item.Master_Label_Id,而不是@item.Label_Id無論是。

選項2

問題是,@Html.EditorFor取決於物業類型 - 因爲你字段string,文本框是選擇的輸入。你將不得不使用不同的字段類型才能使其工作(可能通過傳遞object並安全地將其類型轉換爲期望的類型)。

選項3

我不認爲@Html.CheckBoxFor(x => x.ObsDictionay[item.Label_Id.ToString()] == "Y"? true : false)是有效的原因,因爲我記得你必須通過表達式作爲第一個參數。將其更改爲@Html.CheckBoxFor(x => x.ObsDictionay[item.Label_Id.ToString()], new { Value = Model.ObsDictionay[item.Label_Id.ToString()] == "Y" ? true : false)

+0

選項1仍然不起作用.... – user1414935

+0

選項3 - @ Html。 CheckBoxFor(x => x.ObsDictionay [item.Label_Id.ToString()],new {Value = Model.ObsDictionay [item.Label_Id.ToString()] ==「Y」?true:false) x => x .ObsDictionay [item.Label_Id.ToString()]給出錯誤「不能隱式地將類型字符串轉換爲布爾值」........... 任何幫助?/ – user1414935

+0

@ user1414935 - 你能發佈操作代碼將輸入值綁定到模型屬性的位置? – Kamo