简体   繁体   English

传递给控制器​​时,具有复杂类型的视图模型为null

[英]View model with complex type is null when passed to controller

I am trying to pass a view model with complex types to my controller. 我试图将复杂类型的视图模型传递给我的控制器。 I have researched everything I can top to bottom over this subject and I am still confused. 我已经研究过所有关于这个主题的自上而下的内容,我仍然感到困惑。

The Problem: 问题:

When I click my submit button, the view model is passed in but the List of MacroInfo property is null. 单击我的提交按钮时,将传入视图模型,但是MacroInfo列表属性为null。

UpdateIndexViewModel UpdateIndexViewModel

public class UpdateIndexViewModel
{
    //This view model will become larger later
    public List<MacroInfo> MacrosToUpdate { get; set; }
}

MacroInfo MacroInfo

public class MacroInfo
{
    public bool IsSelected { get; set; }
    public string FullPath { get; set; }
    public string Id { get; set; }
    public DateTime CreatedAt { get; set; }
}   

Controller Action 控制器动作

[HttpPost]
public ActionResult Submit(UpdateIndexViewModel updateIndexViewModel)
{
    //updateIndexViewModel.MacrosToUpdate is null ??
}

Index View 索引视图

@model EplanInterface.Core.ViewModels.UpdateIndexViewModel

@using (Html.BeginForm("Submit", "Update", FormMethod.Post))
{
    <table style="width:100%" , class="table-bordered">
        <thead>
            <tr>
                <th>#</th>
                <th>Macro Path</th>
                <th>Created At</th>
                <th>Update</th>
            </tr>
        </thead>
        @for (int i = 1; i < Model.MacrosToUpdate.Count; i++)
        {
            <tr>
                <td>@Html.TextBoxFor(m =>Model.MacrosToUpdate[i].FullPath)</td>
                <td>@Html.TextBoxFor(m => Model.MacrosToUpdate[i].CreatedAt)</td>
                <td>@Html.CheckBoxFor(b => Model.MacrosToUpdate[i].IsSelected)</td>
            </tr>
        }
    </table>
    <input type="submit" class="btn btn-primary" value="Submit"/>
}

What I have tried 我试过了什么

I tried changing the controller actions property being passed in to List<MacroInfo> macrosToUpdate , but when doing this the property is still null. 我尝试将传入的控制器动作属性更改为List<MacroInfo> macrosToUpdate ,但在执行此操作时,该属性仍为null。

Chrome network inspection Chrome网络检查

在此输入图像描述

Final Remarks 最后的评论

I am not sure if I need to be using an AJAX post to do this, or if my variable names just are not formatted correctly. 我不确定是否需要使用AJAX帖子来执行此操作,或者我的变量名称是否格式不正确。 I am pretty sure it is a binding issue I am not understanding. 我很确定这是一个具有约束力的问题,我不理解。

If anyone could point me in the right direction I would really appreciate it. 如果有人能指出我正确的方向,我会非常感激。

This part of your template is bit wrong. 这部分模板有点不对劲。

@for (int i = 1; i < Model.MacrosToUpdate.Count; i++)
{
    <tr>
        <td>@Html.TextBoxFor(m =>Model.MacrosToUpdate[i].FullPath)</td>
        <td>@Html.TextBoxFor(m => Model.MacrosToUpdate[i].CreatedAt)</td>
        <td>@Html.CheckBoxFor(b => Model.MacrosToUpdate[i].IsSelected)</td>
    </tr>
}

Please change with following and try again. 请更改以下内容,然后重试。

@for (int i = 0; i < Model.MacrosToUpdate.Count; 
{
        <tr>
            <td>@i</td>
            <td>@Html.TextBoxFor(m => m.MacrosToUpdate[i].FullPath)</td>
            <td>@Html.TextBoxFor(m => m.MacrosToUpdate[i].CreatedAt)</td>
            <td>@Html.CheckBoxFor(b => b.MacrosToUpdate[i].IsSelected)</td>
        </tr>
 }

First you were starting the loop with 1, which was the root cause. 首先,你用1开始循环,这是根本原因。 Model binder wasn't able to bind the list properly due to missing zeroth index. 由于缺少第0个索引,模型绑定器无法正确绑定列表。

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

相关问题 ViewModel 复杂对象从视图传递到 controller 时返回 null - ViewModel complex objects returning null when passed from view to controller 当控制器未明确传递给强类型视图时,视图模型为空 - View Model Null when not explicitly passed by controller to strongly typed view 从View传递给Controller的模型数据为NULL - Model Data passed from View to Controller is NULL ASP.NET CORE,查看 model 的所有字段为 null 时传递回 Z9BBF373797BF7CF7BA62C80023 - ASP.NET CORE, View model has all fields as null when passed back to Controller MVC Razor 5:模型从视图中作为null传递给控制器 - MVC Razor 5: Model is passed as null to the controller from a view 提交表单时,在MVC中将空模型传递给控制器 - Null model passed in MVC to controller when form is submitted 传递给 ActionResult 时模型为空 - model is null when passed to ActionResult 我的模型中的空对象由视图返回并传递给控制器​​,尽管它以表格形式输入 - Null object from my model returned by view and passed to controller despite entering it in the form null一直从视图传递到控制器 - null keeps being passed from view to controller 当传递回控制器复选框时,视图模型为空 - View Model null when pass back checkbox to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM