逗人的名單,C# - 渲染複選框,始終返回檢查
請你能幫助下面的問題:
我要呈現在我看來複選框的列表。
@model IEnumerable<CFts.Models.CFModel>
...
@foreach (var test in ViewBag.CF_list)
{
if (test.Text != "" && test.Text != " ")
{
<div class="checkbox">
<label><input value="@test.Value" id="CF_list_" name="CF_list_" @(test.Selected == true ? "checked" : "") type="checkbox"> @test.Text</label>
</div>
}
}
好的,在頁面上的複選框。
CF_list在控制器(SelectListItem)產生
但是問題 - 如果發送該形式中,所有所選擇時間標記的複選框的至少一個。例如:1.我選擇了兩個chekckboxed,發送表單 - 一切正常。 2.我刪除所有勾號和發送表單 - 其中一個複選框(最後單擊)表示爲選中狀態。
爲什麼?
CF_List是SelectListItem
另一個問題:
請你能幫助我理解很簡單的事情
我有我的類模型:
public class VendorAssistanceViewModel
{
public string Name { get; set; }
public bool Checked { get; set; }
}
public partial class CSModel : IEntity
{
public CSModel()
{
VendorAssistances = new[]
{
new VendorAssistanceViewModel { Name = "DJ/BAND" },
new VendorAssistanceViewModel { Name = "Officiant" },
new VendorAssistanceViewModel { Name = "Florist" },
new VendorAssistanceViewModel { Name = "Photographer" },
new VendorAssistanceViewModel { Name = "Videographer" },
new VendorAssistanceViewModel { Name = "Transportation" },
}.ToList();
}
public IList VendorAssistances { get; set; }
我的看法:
@model IEnumerable<CSTS.Models.CSModel>
... some html code...
and how here to show array of checkboxes from Model, using VendorAssistances ?
我知道這很簡單,我看了很多文檔,但還是不明白
謝謝!
非常感謝您的回答。但是,如果我刪除@(test.Selected == true?)選中「:」「如何在頁面上顯示(回發後)該複選框被選中? – Fullbalanced
如果您使用的是asp.net MVC,只需返回張貼的模型到視圖'return View(model)'。但是因爲你正在使用一個viewbag,只需設置'ViewBag.CF_list = model.CF_list_'或者就像你在GET動作中設置它一樣。 –
是的,它被返回在選定的屬性,但這個屬性總是對最後點擊複選框 – Fullbalanced