简体   繁体   English

部分加载视图上的IEnumerable错误

[英]IEnumerable Error on View with Partial Load

I am trying to create a view in my application that performs basic CRUD commands in ASP.NET Core to teach myself some new skills. 我试图在我的应用程序中创建一个视图,该视图在ASP.NET Core中执行基本的CRUD命令,以教会自己一些新技能。 I am however stuck and would appreciate some assistance please. 但是,我被困住了,请提供一些帮助。

I would like to have each "component" of the application sitting in a partial view for maintenance going forward. 我希望应用程序的每个“组件”都处于局部视图中,以进行后续维护。 I initially had my Index view use a declaration of type IEnumerable (for the for each loop): 最初让我的Index视图使用IEnumerable类型的声明(对于每个循环):

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

Which worked perfect for returning the list and rendering the page but then when trying to have my Modal window partially loaded into the page and insert data using the "CreateSample" function on the controller it was not picking up the function and failed the insert (no form action found). 这对于返回列表和呈现页面非常有效,但是当试图将我的“模态”窗口部分加载到页面中并使用控制器上的“ CreateSample”功能插入数据时,它没有拾取该功能并且插入失败(否找到表单动作)。 If I then try to add: 如果我然后尝试添加:

@model Project.Web.Models.Sample.SampleModel

to the CreateModal view page it throws an error and wont even let me render the page, I presume because its being partial loaded the app is seen as having two SampleModel declarations. 到CreateModal视图页面时,它会引发错误,甚至不会让我呈现该页面,我认为是因为部分加载该应用程序被视为具有两个SampleModel声明。 If I create this page completely separate and not partially loaded with the normal @model declaration it works. 如果我完全分开创建此页面,并且未部分加载正常的@model声明,则它将正常工作。

I have the basic setup going so far and have included my code for each below. 到目前为止,我已经完成了基本设置,并在下面包含了我的代码。

Model - SampleModel 型号-SampleModel

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

Controller - SampleController 控制器-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);
        }

Views - Index, View, Create 视图-索引,视图,创建

Index - Calls Partial Views for View and Create 索引 -调用局部视图进行查看和创建

@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")

View - Foreach to Loop Records 查看 -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>

Create - Insert New Record 创建 -插入新记录

@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>

As per Ammar's comment, you've just copy-pasted the Index Controller's data access. 根据Ammar的评论,您刚刚复制粘贴了索引控制器的数据访问权限。 When building a form allowing the user to create a single new item, then the pattern is to typically pre-instantiate an empty model and pass it to the view: 构建允许用户创建单个新项目的表单时,模式通常是预先实例化一个空模型并将其传递给视图:

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

    return PartialView("_CreateModal", model);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM