簡體   English   中英

如何從ASP.NET CheckBoxs中獲得多個選擇

[英]How can I get multiple selections from ASP.NET CheckBoxs

這看起來應該很簡單-但我無法使其正常工作!

我的模型中有一個枚舉,我想將其顯示為復選框列表。 用戶可以選擇多個復選框,我想將其保存在數據庫中。

所以枚舉是這樣的(大約20個元素未刪節):

public enum ReferrerType
{
    [Description("No Data")]
    NoData = 9999,
    [Description("Father or Mother")]
    Parents = 1,
    [Description("Brother or Sister")]
    Sibling = 2,
    [Description("Other")]
    Other = 10
}

其中,描述是在UI上顯示的內容,數字值是要保存在數據庫中的內容。 數字必須直接保留在列表中,因為它們直接進入統計數據包。

然后,我定義了Referrer類:

public class Referrer
{
    public virtual Guid Id { get; private set; }
    public virtual ReferrerType{ get; set; }
}

我意識到這可能是一個奇怪的(反)模式。 我匆忙開發了它,並在閑暇時悔改。 任何有關改進此模型的建議也將不勝感激!

我的控制器設置了列表:

private static IList<string> GenerateReferrerList()
{
    var values = from ReferrerType e in Enum.GetValues(typeof(ReferrerType))
                 select new { Name = e.ToDescription() };

    return values.Select(x => x.Name).ToList();
}

我在視圖中這樣使用它:

<div class="radio-boolean form-field" id="Referrers">
      <p class="form-field-attribute"> <span class="label">Referred By </span> </p>
      <% for (var i = 0; i < ((IList<string>)ViewData["ReferrerList"]).Count; i++)
      { %>
           <p class="form-field-value">
           <%= Html.CheckBox(string.Format("Referrers[{0}].Type", i) ) %>
           <label for="Referrers"> <%= ((IList<string>)ViewData["ReferrerList"])[i]%></label>
       </p>
</div>

而且它不起作用! 我想我缺少明顯的東西,但是我無法解決。 沒有錯誤-應該是引薦來源網址為空的數據庫表...

一如既往,任何幫助深表感謝!

讓我們花點時間在這里看看我們需要什么。 我們需要顯示一個表單,其中包含多個復選框(每個枚舉的值一個)和一個關聯的標簽(此標簽應來自枚舉上的Description屬性)。 提交此表單后,我們希望獲取使用檢查過的所有值。

因此,一如既往,一旦我們對要執行的操作有了明確的定義,就可以引入視圖模型:

public class MyViewModel
{
    public bool IsChecked { get; set; }
    public ReferrerType ReferrerType { get; set; }
    public string Text { get; set; }
}

然后我們編寫一個控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = Enum.GetValues(typeof(ReferrerType)).Cast<ReferrerType>().Select(x => new MyViewModel
        {
            ReferrerType = x,
            Text = x.ToDescription() // I guess that's an extension method you wrote
        });
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<MyViewModel> model)
    {
        ...
    }
}

最后是一個與我們的控制器的Index操作相對應的強類型視圖(〜/ Views / Home / Index.aspx):

<% using (Html.BeginForm()) { %>
    @Html.EditorForModel()
    <input type="submit" value="OK" />
<% } %>

最后一部分是相應的編輯器模板( ~/Views/Home/EditorTemplates/MyViewModel.ascx ):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
<%= Html.CheckBoxFor(x => x.IsChecked) %>
<%= Html.HiddenFor(x => x.ReferrerType) %>
<label><%: Model.Text %></label>

現在,當在POST索引操作中提交此表單時,您將獲得所有枚舉的列表,並帶有一個相應的布爾值,指示用戶是否檢查了它。

OT:請勿執行過多的操作:

return (from e in Enum.GetValues(typeof(ReferrerType))
        select e.ToDescription()).ToList();

要不就

return Enum.GetValues(typeof(ReferrerType)).Select(e => e.ToDescription()).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM