Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC视图返回空对象_C#_Asp.net Mvc_Razor_Binding - Fatal编程技术网

C# ASP.NET MVC视图返回空对象

C# ASP.NET MVC视图返回空对象,c#,asp.net-mvc,razor,binding,C#,Asp.net Mvc,Razor,Binding,我有下面的代码,但它返回一个空的FormationDTO对象,我做错了什么吗? 我不明白为什么它不能正确地将FormationFormViewModel的FormationDTO绑定到动作参数FormationDTO,因为它在其他控制器中工作 格式化控制器 FormationForm.cshtml 如果我正确理解代码。看起来您应该将FormationFormViewModel传递给控制器操作。不格式化。请查看页面上生成的HTML。我猜输入元素上的name属性看起来像formationDTO.na

我有下面的代码,但它返回一个空的FormationDTO对象,我做错了什么吗? 我不明白为什么它不能正确地将FormationFormViewModel的FormationDTO绑定到动作参数FormationDTO,因为它在其他控制器中工作

格式化控制器

FormationForm.cshtml


如果我正确理解代码。看起来您应该将FormationFormViewModel传递给控制器操作。不格式化。请查看页面上生成的HTML。我猜输入元素上的name属性看起来像formationDTO.name,因为您的ViewModel是FormationFormViewModel。但是后端的ModelBinder将只查找属性名,因为您正在尝试构建FormationDTO

您可能需要手动创建这些输入元素,或者使用子操作将正确的ViewModel获取到一个视图中,该视图允许您使用razor@Html helpers来构建正确的元素


或者,更简单的选择是让您的控制器操作接受FormationFormViewModel,然后ModelBinder应该根据您的需要正确构建FormationD的属性。

谢谢您的回答,但我不明白为什么不以这种方式绑定。我在另一个控制器上做了完全相同的事情,使用包含多个对象的viewmodel并在POST操作中检索其中一个对象,效果非常好。@ArmelMercier,在视图中,当您有一个form and do model=>model时。//无论如何,它是绑定和设置视图的模型,不管传递到该视图中的是什么。因此,当您提交表单时,您正在将该模型提交给操作。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(FormationDTO formation)
{
    if (!ModelState.IsValid){
        return View("FormationForm", new FormationFormViewModel { FormationDTO = formation, Categories = GetCategories() });
    }
    else{
        // DO THE STUFF
    }

}
@model BSS_IT_Education.Models.FormationFormViewModel

@{ 
    ViewBag.Title = "Formation";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Save", "Formations"))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.FormationDTO.Id)

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.FormationDTO.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.FormationDTO.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Entrez le nom de la formation..." } })
                @Html.ValidationMessageFor(model => model.FormationDTO.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        // BUNCH OF OTHERS FORM-GROUPS

        <div class="form-group">
            <div class="col-md-offset-2 col-md-8">
                <button type="submit" class="btn btn-success">@((Model.FormationDTO.Id == 0) ? "Sauvegarder  " : "Modifier")</button>                
            </div>
        </div>
    </div>
}