POST方法中ViewModel的List <model>为null

时间:2018-06-21 20:03:25

标签: asp.net-mvc viewmodel

我正在研究以下内容。运行代码时,我可以通过GET方法以正确的形式从数据库中获取数据,并且可以在View中正确显示数据。但是,在编辑/更改某些数据(例如Type并按Save Data时,会调用POST方法,但是参数的studentsViewModel值是{{ 1}}。不知道为什么。

我在网站和其他在线地方也遇到了类似的问题。但是他们没有我要的东西。另外,大多数都使用null

以下是代码段:

控制器

HTMLHelpers

Details.cshtml

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)
        {
            ....
        }

        ....
    }
}

模型

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

StudentsViewModel.cs

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; }

}

编辑:添加类型类

Type.cs

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

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

1 个答案:

答案 0 :(得分:0)

您不应将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>