2012-08-30 95 views
0

所以我是MVC和ASP.Net的新手。我整個下午都在閱讀這裏的帖子,我無法得到這個工作。我試過this,thisthis,我只是不知道我在做什麼錯。Checkboxfor not binding model

條件類

public class conditions 
{ 
    public string condName; 
    public bool condValue; 

    public conditions(string s) 
    { 
     condName = s; 

    } 
} 

風險歷史課

public class riskHistory 
{ 
    public List<conditions> results; 
    public riskHistory() 
    { 
     results = new List<conditions>(); 
     results.Add(new conditions("History of Marfans Syndrome")); 
     results.Add(new conditions("Family history of aortic disease")); 
     results.Add(new conditions("Recent aortic manipulation")); 
     results.Add(new conditions("Known thorasic aorta aneurysm")); 

    } 
} 

現在有定義的相同方式riskPain和riskFeatures類。我需要將它分成3個,因爲每個都必須以不同的方式處理。我在視圖中顯示每個獨特類的內容。

視圖模型定義爲

public class adViewModel 
    { 
     public riskFeatures rF; 


public riskPain rP; 
    public riskHistory rH; 

    public adViewModel() 
    { 
     rF = new riskFeatures(); 
     rH = new riskHistory(); 
     rP = new riskPain(); 
    } 
} 

然後我在視圖中該代碼。這是adViewModel的強類型。

Does the patient have any of the following history? 
@for (int x =0; x<Model.rH.results.Count; x++) 
{ 
    string n = Model.rH.results[x].condName.ToString(); 

    <b>@Html.Label(n)</b> @Html.CheckBoxFor(m => m.rH.results[x].condValue) 
} 



Does the patient have any of the following features to their pain? 

//繼續到其他類

的視圖顯示正確的,但是當我提交表單它不綁定(所有condvalue的都是假的)。如果你要建議編輯模板,那麼我還有第二個問題。在for循環的視圖代碼中的問題「患者是否有以下病史?」對於每組複選框,該問題必須有所不同。這將如何工作?

謝謝噸傢伙! 〜Chad

+0

您的控制器方法是用[HttpPost]修飾的方式提交的?此外,我建議你使用JavaScript與複選框顯示某些問題。 – AwDogsGo2Heaven

回答

0

因此,使用像CheckBoxFor這樣的Html幫助器for循環對我造成問題,因爲我沒有控制生成的輸入標記的id。檢查呈現的最終標記並查看它正在製作的控件的ID。寫出控件的html並不是那麼長,然後調用輔助方法。

+0

謝謝@innerexception你是對的。現在再來看一個問題,示例html標籤將如何查找該示例? – Chad