【问题标题】:how to get selected checkbox in asp.net using entity framework如何使用实体框架在asp.net中获得选中的复选框
【发布时间】:2016-03-04 00:01:03
【问题描述】:

我是 asp.net mvc 的新手。我有一个复选框列表,我希望在选中复选框时显示一个新的选定复选框列表。 我的代码 Product.cs 代码:

public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public int Price { get; set; }
    public bool Checked { get; set; }
    public virtual ICollection<Purchase> Purchases { get; set; }
}

我的看法:

<h2>Product Lists</h2>
@using (Html.BeginForm())
{

    <table class="table">
        <tr>
            <th>
                Product ID
            </th>
            <th>
                Product Name
            </th>
            <th>
                Price
            </th>
            <th></th>
        </tr>
        @for (var i = 0; i < Model.Count(); i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(x => x[i].ProductID)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].ProductName)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].Price)
                </td>
                <td>
                    @Html.CheckBoxFor(x => x[i].Checked, new { Style = "vertical-align:3px}" })
                </td>
            </tr>
        }

    </table>

    <input type="submit" value="Purchase" class="btn btn-default" />
}

这是我的控制器代码。我希望在新页面中选中复选框时显示选中的复选框。 我的 ActionResult:

    public ActionResult Index()
    {
        return View(db.Products.ToList());
    }

    [HttpPost]
    public ActionResult Index(List<Product> list)
    {
        return View(list);
    }
@using (Html.BeginForm())

{

<table class="table">
    <tr>
        <th>
            Product ID
        </th>
        <th>
            Product Name
        </th>
        <th>
            Price
        </th>
        <th></th>
    </tr>

    @for (var i = 0; i < Model.Count(); i++)
    {
        <tr>
            <td>
                @Html.DisplayFor(x => x[i].ProductID)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].ProductName)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Price)
            </td>
            <td>
                @Html.CheckBoxFor(x => x[i].Checked, new { Style = "vertical-align:3px}" })
            </td>
        </tr>
    }

</table>

【问题讨论】:

  • this answer为例
  • 请多多帮助我..我无法解决我的问题
  • 什么问题?什么不工作?你遇到了什么错误?
  • 您是否遇到任何错误?如果你调试你的索引POST,你的列表中有Checked == true吗?
  • @ThiagoFerreira 是的,我在列表中检查了==true。但我想在新视图中显示这些真实检查。但我不知道该怎么做???!!!

标签: asp.net-mvc entity-framework-4


【解决方案1】:

如果您已经有一个包含所有选中/未选中属性的列表,并且只想在新视图中显示选中的记录,您可以将列表存储在 TempData 中并重定向到将使用您的列表的操作:

public ActionResult Index()
{
    return View(db.Products.ToList());
}

[HttpPost]
public ActionResult Index(List<Product> list)
{
    TempData["CheckedRecords"] = list.Where(x=>x.Checked).ToList(); //Don't forget to add 'using System.Linq;'!
    return RedirectToAction("MyOtherView");
}

public ActionResult MyOtherView()
{
    var checkedRecords = (List<Product>)TempData["CheckedRecords"];
    return View(checkedRecords);
}

【讨论】:

  • 如果用户刷新浏览器,这一切都会失败。将数据保存到数据库!
猜你喜欢
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
  • 2013-10-20
  • 2010-10-05
  • 1970-01-01
相关资源
最近更新 更多