【问题标题】:How to pass array of IEnumerable<int> from one view to another in ASP.NET MVC correctly?如何在 ASP.NET MVC 中正确地将 IEnumerable<int> 数组从一个视图传递到另一个视图?
【发布时间】:2020-09-17 12:24:48
【问题描述】:

在下面的代码中,我创建了一个索引视图,使用户能够检查他们想要更新的记录。当点击提交按钮时,他们应该被重定向到一个网页,并且需要执行一些代码来更新这些记录。

以下是我实现的代码:

观点:

@model IEnumerable<BulkDelete.Models.Employee>

@{int[] employeesToUpdate;}
    <div style="font-family:Arial">
        @using (Html.BeginForm("UpdateMultiple", "Home", FormMethod.Post))
        {
            <table class="table">
                <thead>
                    <tr>
                        <td>Checkbox<br /></td>

                        <th>Name</th>
                        <th>Gender</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>

                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                <input type="checkbox" name="employeesToUpdate" id="employeesToUpdate" value="@item.ID" />
                            </td>
                            <td>@item.Name</td>
                            <td>@item.Gender</td>
                            <td>@item.Email</td>
                        </tr>
                    }
                </tbody>
            </table>

            <input type="submit"  value="update selected employees" />
        }
    </div>

控制器:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BulkDelete.Models;

namespace BulkDelete.Controllers
{
    public class HomeController : Controller
    {
        SampleDBContext db = new SampleDBContext();
        public  System.Web.SessionState.HttpSessionState Session { get; }

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

        [HttpPost]
        public ActionResult UpdateMultiple(IEnumerable<int> employeesToUpdate)
        {
            return RedirectToAction("UpdateMultipleRecords");
        }
        
        //[HttpPost]
        public ActionResult UpdateMultipleRecords()
        {
            IEnumerable<int> employeesToUpdate = (IEnumerable<int>)TempData["employeesToUpdate"];
            var listemployee = db.Employees.Where(x => employeesToUpdate.Contains(x.ID));

            foreach (var item in listemployee)
            {
                int itemid = item.ID;

                Employee e = db.Employees.Find(itemid);
                e.Email = "hello.";
                // e = db.Employees.Find(item);
                db.Entry(e).State = EntityState.Modified;
            }

            db.SaveChanges();

            return RedirectToAction("Index");
        }
    }
}

我一遍又一遍地遇到同样的错误:

无法创建类型为“System.Collections.Generic.IEnumerable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'的空常量值。

在此上下文中仅支持实体类型、枚举类型或原始类型。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NotSupportedException:无法创建类型为 'System.Collections.Generic.IEnumerable`1[[System.Int32,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089 的空常量值]]'。 此上下文仅支持实体类型、枚举类型或原始类型。

【问题讨论】:

    标签: c# asp.net-mvc entity-framework


    【解决方案1】:

    您的表单发布方法需要此签名,您似乎也依赖于 RedirectToAction 中的 TempData,因此您需要在您的发布方法中设置 TempData:

        [HttpPost]
        public ActionResult UpdateMultiple(IEnumerable<int> employeesToUpdate)
        {
            //Set the TempData
            TempData["employeesToUpdate"] = employeesToUpdate;
            return RedirectToAction("UpdateMultipleRecords");
        }
    

    在您看来,您需要像这样修改复选框元素上的名称属性,因为它是一个IEnumerable,因此需要对名称进行索引,此外,目前您在所有复选框上都使用相同的id , id 应该对每个元素都是唯一的,因此您可以使用iter 变量附加到复选框的id

    @{int[] employeesToUpdate;} @*This doesn't look like it's necessary*@
    @{int iter = 0;} @*Declare an iterator variable*@
    

    .......

    @for (var item in Model)
     {
        <tr>
            <td>
               @*index the controller parameter's IEnumerable*@
               <input type="checkbox" name="employeesToUpdate[@iter]" id="employeesToUpdate_@iter" value="@item.ID" /> 
            </td>
            <td>@item.Name</td>
            <td>@item.Gender</td>
            <td>@item.Email</td>
       </tr>
       iter++;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多