首页 > 解决方案 > 部分负载视图上的 IEnumerable 错误

问题描述

我正在尝试在我的应用程序中创建一个视图,该视图在 ASP.NET Core 中执行基本的 CRUD 命令,以自学一些新技能。然而,我被困住了,希望能得到一些帮助。

我想让应用程序的每个“组件”都处于局部视图中,以便继续进行维护。我最初让我的索引视图使用 IEnumerable 类型的声明(对于每个循环):

@model IEnumerable<Project.Web.Models.Sample.SampleModel>

这非常适合返回列表和呈现页面,但是当尝试将我的模态窗口部分加载到页面中并使用控制器上的“CreateSample”函数插入数据时,它没有选择该函数并且插入失败(否找到表单操作)。如果我再尝试添加:

@model Project.Web.Models.Sample.SampleModel

到 CreateModal 视图页面,它会引发错误,甚至不会让我渲染页面,我认为是因为它被部分加载,应用程序被视为具有两个 SampleModel 声明。如果我创建这个页面完全独立并且没有部分加载正常的@model 声明,它就可以工作。

到目前为止,我已经完成了基本设置,并在下面包含了我的代码。

模型 - SampleModel

public class SampleModel
    {
        public int Id { get; set; }
        public string SampleText { get; set; }
    }

控制器 - SampleController

public class SampleController : Controller
    {
        public const string ControllerName = "Sample";
        //Open Database Connection
        private _DBContext DBDatabase = new _DBContext ();

        public ActionResult Index()
        {
            var Model = DBDatabase.Sample.Select(s => new SampleModel
            {
                Id = s.Id,
                SampleText = s.SampleText
            }).ToList();

            return PartialView(Model);
        }

        [ActionName("_CreateModal")]
        public ActionResult InsertNewRecord()
        {
            var Model = DBDatabase.Sample.Select(s => new SampleModel
            {
                Id = s.Id,
                SampleText = s.SampleText
            }).ToList();

            return PartialView("_CreateModal", Model);
        }

视图 - 索引、视图、创建

索引- 调用局部视图进行查看和创建

@using Project.Web.Controllers
@model Project.Web.Models.Sample.SampleModel
<!--html stuff here -->
@await Html.PartialAsync("_CreateModal")
<!--more html stuff here -->
@await Html.PartialAsync("_ViewData")

查看- Foreach 到循环记录

@model Project.Web.Models.Sample.SampleModel

<table style="width: 100%;" id="example">
    <thead>
        <tr>
            <th>#</th>
            <th>Sample Text</th>
            <th class="text-center">Status</th>
            <th class="text-center">Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var sample in Model)
        {
            <tr>
                <th scope="row">@sample.Id</th>
                <td>@sample.SampleText</td>
                <td class="text-center">
                    <div class="badge badge-success">Active</div>
                </td>
                <td class="text-center">
                    <div role="group" class="btn-group-sm btn-group">
                        <button class="btn-shadow btn btn-primary">Edit</button>
                        <button class="btn-shadow btn btn-primary">Delete</button>
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>

创建- 插入新记录

@model Project.Web.Models.Sample.SampleModel

<form method="post" asp-action="/SampleModel/CreateSample">
                    <div class="form-group">
                        <label for="CreationTime">SampleText</label>
                        <div>
                            <input type="text" class="form-control" id="SampleText" name="SampleText" placeholder="SampleText">
                        </div>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">Sign up</button>
                    </div>
                </form>

标签: c#asp.net-coremodel-view-controllerpartial-views

解决方案


根据 Ammar 的评论,您刚刚复制粘贴了索引控制器的数据访问权限。在构建允许用户创建单个新项目的表单时,模式通常是预先实例化一个空模型并将其传递给视图:

[ActionName("_CreateModal")]
public ActionResult InsertNewRecord()
{
    var model = new SampleModel(); // If Id is a GUID, then you could assign one here

    return PartialView("_CreateModal", model);
}

推荐阅读