【问题标题】:Asp.net Core MVC - on form posting to controller IEnumerable model in controller action is emptyAsp.net Core MVC - 在表单发布到控制器操作中的控制器 IEnumerable 模型为空
【发布时间】:2018-05-03 09:06:54
【问题描述】:

当我尝试将 IEnumerable 从 razor 视图发布到 Controllor 操作方法时遇到问题。如果我使用 List,结果也是一样的。 我也在评论中发布了我的控制器操作方法。在我的控制器操作方法中,我得到的列表是空的。

这是我的观点:

@model IEnumerable<Subject>
<form asp-action="AddNewSubjects" asp-controller="Teacher" method="post" role="form" class="form-horizontal">
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Number of class</th>
                <th>Level</th>
            </tr>
        </thead>
        <tbody>
            @if (Model != null)
            {

    var item = Model.ToList();
    @for(int i=0;i<Model.Count();i++)
                {
            <tr>
                <td>@item[i].ID</td>
                <td>@item[i].Name</td>
                <td>@item[i].ClassNumber</td>
                <td>@item[i].Level</td>

            </tr>

                }
            }
        </tbody>
    </table>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-5">
            <input type="submit" class="btn btn-primary" value="Save all subjects" />
        </div>
    </div>
</form>

这是我的控制器:

            private readonly ISubjectService _subjectService;

            public TeacherController(ISubjectService subjectService)
            {
                _subjectService= subjectService;
            }

     [HttpPost]
            public IActionResult AddNewSubjects(IEnumerable<Subject> subjects)
            {
                var newSubjects= (from p in subjects
                                    where p.State== Status.New
                                    select p);
                    var result = _subjectService.SaveTeacherSubjects(newSubjects);
                return View("ProfesorPages");   
            }

【问题讨论】:

  • 这是我的控制器方法: [HttpPost] public IActionResult AddNewSubjects(IEnumerable subject) { var newSubjects= (from p in subject where p.State== Status.New select p); var 结果 = _subjectService.SaveTeacherSubjects(newSubjects); return View("ProfesorPages"); } 返回视图(“ProfesorPages”); }

标签: c# asp.net-core-mvc asp.net-core-2.0


【解决方案1】:

我不知道你想在这里做什么。除了提交按钮,您的表单没有任何输入元素。当然,您没有看到任何回帖。

@model IEnumerable<Subject>

<form>
    ...
    <tbody>
        @for(int i = 0; i < Model.Count(); i++)
        {
            <tr>
                <td>
                    <input type="hidden" asp-for="Model[i].ID" />
                </td>
                <td>
                    <input type="text" asp-for="Model[i].Name" />
                </td>
                ...
            </tr>
        }
    </tbody>
    ...
</form>

为什么??

  • 为什么将 IEnumerable 转换为名为 item 的列表?为什么不直接列举你的主题?
  • 为什么不创建一组称为 ViewModel 的不同模型并将其传递给 View,而不是直接在 View 上使用数据库中的模型?

【讨论】:

  • 我没有使用数据库中的模型。我调用我的 web api 并加载所有科目的列表,并且我有一个部分视图,教师可以在其中添加新科目。添加所有新主题时,我调用 web api 并保存所有新主题。我将 razor 中的 @model 更改为 IList,并按照您的建议添加输入类型,现在它可以工作了!
  • IEnumerable 应该可以正常工作。也可能我弄错了,但Subject 似乎是数据库中的模型。无论如何,很高兴这对你有用:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
相关资源
最近更新 更多