【问题标题】:List<model> of ViewModel is null in POST methodViewModel 的 List<model> 在 POST 方法中为空
【发布时间】:2018-12-01 06:23:35
【问题描述】:

我正在做以下工作。当我运行代码时,我可以通过GET 方法从数据库中以正确的形式获取数据,并且可以在View 中正确显示它。但是,在编辑/更改某些数据时,比如Type,并在按下Save Data 时,会调用POST 方法,但作为参数的studentsViewModel 值是null。不知道为什么。

我在网站和网上其他地方也遇到过类似的问题。但他们没有我正在寻找的东西。另外,他们中的大多数人都在使用HTMLHelpers

这里是sn-ps的代码:

控制器

public IActionResult StudentsDetails(int Id)
{
    try
    {
        var typeItems = _context.Type.Where(t => t.IsActive == true).Select(us => new SelectListItem { Value = us.TypeId.ToString(), Text = us.TypeName.ToString() }).ToList();
        ViewBag.TypeItems = typeItems;

        var studentsData = _context.Students
            .Where(r => r.StudentId == Id).ToList();

        var studentsViewModelData = new tudentsViewModel();

        studentsViewModelData.Students = studentsData;
        studentsViewModelData.StudentId = Id;

        return View(studentsViewModelData);
    }
    catch (Exception ex)
    {
        ....      
    }

    ....
}

[HttpPost]
public IActionResult StudentsDetails(int Id, StudentsViewModel studentsViewModel)
{
    try
    { 
        if(ModelState.IsValid)
        {
            ...
            studentsViewModel.StudentId = Id;
            ....     
        }
        catch(Exception ex)
        {
            ....
        }

        ....
    }
}

Details.cshtml

<div class="card-body card-padding">
    <form method="post" asp-action="Details" asp-controller="Students" id="studentsForm">
        ....
        <table id="data-table-basic" class="table table-striped table-vmiddle">                    
            @if(Model.Students.Count() != 0)
            {
                <thead>
                    <tr>
                        <th data-column-id="StudentNumber">Student Number</th>
                        <th data-column-id="Type">Student Type</th>
                    </tr>
                </thead>
            }
            <tbody>
                @for(int count = 0;count < Model.Students.Count; count++)
                {
                    <tr>
                        <td>                                           
                            @Model.Students[count].StudentNumber
                        </td>
                        <td>
                            <select asp-for="@Model.Students[count].TypeId" asp-items="@ViewBag.TypeItems" class="chosen" data-placeholder="Select student type" name="selectType" id="selectType">
                                <option value=""></option>
                            </select>        
                        </td>
                    </tr>                                          
                }
            </tbody>                       
        </table>
        <div>
            <button type="submit" value="Save">Save Data</button>
        </div>
    </form>
</div>

型号

public class Students
{
    [Key]
    public int StudentId { get; set; }

    public string StudentNumber { get; set; }

    [ForeignKey("Type")]
    public int TypeId { get; set; }
    public virtual Type Type { get; set; }

}

StudentsViewModel.cs

public class StudentsViewModel
{
    public int StudentId { get; set; }

    public List<Students> Students { get; set; }  
}

编辑:添加类型类

Type.cs

public class Type
{
    [Key]
    public int TypeId { get; set; }

    public string TypeName { get; set; }
    public bool IsActive { get; set; }
}

【问题讨论】:

  • 能否在参数中添加[FromBody] 属性并重试? public IActionResult StudentsDetails(int Id, [FromBody] StudentsViewModel studentsViewModel)
  • @Win 在进行更改后运行代码时,只要我按下Submit Data,页面就会显示:此页面无法正常工作`并给我一个HTTP ERROR 415
  • @Dashamlav 415 是不受支持的媒体类型 - 正在发送什么?您可以在浏览器中查看开发工具的网络选项卡并发布其中的内容吗?感觉需要更多的页面标记来复制您的问题并提供答案。
  • @Dashamlav 我注意到操作方法名称与asp-action="Details"asp-action="StudentsDetails" 不匹配。这是您的问题中的错字吗?否则,您甚至无法使用操作方法。你能告诉我们StudentsTable类吗?
  • @Win 打错字了!

标签: asp.net-mvc viewmodel


【解决方案1】:

您不应将 id 和 name 明确设置为 select。删除name="selectType" id="selectType"

<select asp-for="@Model.Students[count].TypeId" 
        asp-items="@ViewBag.TypeItems"
        class="chosen" 
        data-placeholder="Select student type">
    <option value=""></option>
</select>  

【讨论】:

  • 我已删除 nameid。在运行代码时,我仍然收到HTTP ERROR 415
  • 而且,顺便提一下,在添加[FromBody] 时,现在POST 方法在按下Save Data 时无法获得他。按下按钮时,我只是收到415 错误。
  • 对不起,请继续删除[FromBody]
  • 我删除了[FromBody]。我没有收到415 错误。 POST 方法被击中,但 Students 即将为空。
  • 你能在浏览器中显示渲染的 html 代码吗?
猜你喜欢
  • 1970-01-01
  • 2019-11-24
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2015-01-13
相关资源
最近更新 更多