2011-05-20 55 views
1

因此,我有一個視圖模型調用ProductViewModel,它有一個產品可以生產的網站列表。我試圖弄清楚如何使用複選框爲用戶選擇產品可以生產的網站,在表單上顯示此信息。看起來挺直的,對吧?那麼它不工作就像我想/需要。希望有人能幫助我指導正確的做法。無法在表單上綁定html.checkbox

我的課:

public class ProductViewModel 
{ 
    public List<Sites> SiteList {get; set;} 
    public string ProductName {get; set;} 
    public int ProductId {get; set;} 
    public User ProductOwner{get; set;} 
} 

public class Sites 
{ 
    public int SiteId {get; set;} 
    public string SiteName {get; set;} 
    public bool IsSelected {get; set;}  
} 

我的部分觀點:

@Html.LabelFor(m=>m.Sites): 
@foreach (var site in Model.Sites) 
{ 
    @Html.CheckBox("Sites", site.IsSelected, new { value = site.SiteName }) 
    @Html.Label(site.SiteName) 
} 

當使用@ Html.Checkbox()我看到在HTML下面的輸出從瀏覽器:

<input checked="checked" id="Sites" name="Sites" type="checkbox" value="Miami" /> 
<input name="Sites" type="hidden" value="false" /> 

我明白隱藏的領域,但我真正需要的是獲得所選項目的價值。所以我需要找回邁阿密的名單。我不需要html助手似乎想要發送的錯誤/真實的東西(即邁阿密= true)

所以我試了這個。

@for(int id=0; id < Model.Sites.Count(); id++) 
{ 
    <input type="checkbox" id="@Model.Sites[id].SiteName" name="Sites[@id]" value="@Model.BoxingSites[id].SiteName" @(Model.Sites[id].IsSelected ? @"checked=""checked""": "") /> 
    @Html.Label(Model.Sites[id].SiteName)     
} 

,輸出是:

<input type="checkbox" id="Miami" name="Sites[0]" value="Miami" checked=&quot;checked&quot; /> 
<label for="Miami">Miami</label> 

在這兩種情況下,我不能夠得到粘結劑張貼到行動時,表單值映射到Product.Sites列表。

的操作是這樣的:

[HttpPost] 
public ActionResult Edit(ProductViewModel Product) 
{ 
    //Does something with the form data. 
} 

其他值(產品名稱等)映射罰款。

我在做什麼錯?我覺得我錯過了一些東西,因爲MVC簡化了很多其他形式的處理情況,這應該會更容易一些。

在此先感謝...

+0

,你也可以使用CheckBoxList的助手從這裏http://awesome.codeplex.com/ – Omu 2011-05-20 13:11:24

+0

@歐姆 - 感謝您的參考。這真的很棒。我也肯定會將其用於其他一些需求。 – Racter 2011-05-20 16:32:04

回答

2

如何使用編輯器的模板,而不是與循環掙扎:

@model ProductViewModel 
@using (Html.BeginForm()) 
{ 
    ... some other form fields 

    @Html.LabelFor(x => x.SiteList) 
    @Html.EditorFor(x => x.SiteList) 

    <input type="submit" value="Create" /> 
} 

和相應的編輯模板中~/Views/Shared/EditorTemplates/Sites.cshtml

@model Sites 
<div> 
    @Html.HiddenFor(x => x.SiteId) 
    @Html.CheckBoxFor(x => x.IsSelected) 
    @Html.LabelFor(x => x.SiteName) 
</div> 

現在不只有您的視圖代碼更乾淨,但會爲輸入字段生成專有名稱,以便模型聯編程序可以將選擇在POST操作中返回ed值。

[HttpPost] 
public ActionResult Create(ProductViewModel model) 
{ 
    ... 
} 
+0

您剛剛向我展示了何時使用模板的一個很好的示例。這有效;不過,我在標籤周圍看到一個奇怪的問題。 @ Html.LabelFor(x => x.SiteName)發送「SiteName」而不是「Miami」 任何想法爲什麼? – Racter 2011-05-20 16:26:39

+0

哦,我還注意到另一個問題。發佈時,SiteID映射,IsSelected工作,但SiteName爲空。即使在打開@ Html.Label(Model.SiteName)之後。 – Racter 2011-05-20 16:30:32

+0

@Racter,它是空的,因爲它永遠不會被髮送到服務器。您可以將它作爲隱藏字段包含在模板中:'@ Html.HiddenFor(x => x.SiteName)',就像您使用'SiteId'一樣。至於你的第一個問題,你可以用@ Html.DisplayFor(x => x.SiteName)替換@ Html.LabelFor(x => x.SiteName)'如果你想顯示值。 – 2011-05-20 16:33:41

0

這是什麼在爲我工作。

// View Model 
[Display(Name="Boolean Property")] 
[UIHint("booleancheckbox"] 
public bool? booleanProperty; 

查看

// View 
@Html.EditorFor(m => m.booleanProperty, new { @onclick = "Toggle(this);" }) 

編輯模板 - 添加一些更多的代碼來處理空值

// Editor Template booleancheckbox.cshtml 
@model bool? 

@{ 
    labelText = ViewData.ModelMetadata.DisplayName != null ? 
       ViewData.ModelMetadata.DisplayName : 
       ViewData.ModelMetadata.PropertyName; 
} 

<label for="@ViewData.ModelMetadata.PropertyName">@labelText 

    @Html.CheckBox(string.Empty, Model.Value, ViewContext.ViewData) 

</label>