Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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# 我的viewmodel集合不是';t绑定_C#_Asp.net_Asp.net Mvc_Razor - Fatal编程技术网

C# 我的viewmodel集合不是';t绑定

C# 我的viewmodel集合不是';t绑定,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我不明白为什么这不起作用 这是我模型的一部分: public class AddComplianceReportViewModel { public PartyViewModel[] InvolvedParties { get; set; } public PartyViewModel[] ManagementParties { get; set; } public ConcealPartyViewModel[] ConcealParties { get; set; }

我不明白为什么这不起作用

这是我模型的一部分:

public class AddComplianceReportViewModel 
{
    public PartyViewModel[] InvolvedParties { get; set; }
    public PartyViewModel[] ManagementParties { get; set; }
    public ConcealPartyViewModel[] ConcealParties { get; set; }

    public string ReportedByFirstName { get; set; }
    public string ReportedByLastName { get; set; }
    public string ReportedByBestTimeToContact { get; set; }
    public bool ReportedByRemainAnonymous { get; set; }

    [Phone]
    public string ReportedByPhone { get; set; }

    [EmailAddress]
    public string ReportedByEmail { get; set; }

    public string GeneralDescription { get; set; }
    public string DetaledDescription { get; set; }
}

public class PartyViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
}

public class ConcealPartyViewModel : PartyViewModel
{
    public string ConcealmentDescription { get; set; }
}
[HttpGet, Route("report/new")]
public ViewResult FileReport()
{
    var vm = new AddComplianceReportViewModel()
    {
        ReportedByFirstName = "amy",
        ReportedByLastName = "lastname",
        ReportedByEmail = "my@email.com",
        ReportedByBestTimeToContact = "best time",
        ReportedByPhone = "555-123-4567",
        ReportedByRemainAnonymous = false,
        InvolvedParties = new []
        {
            new PartyViewModel()
            {
                FirstName = "saul",
                LastName = "goodman",
                Title = "lawyer"
            },
            new PartyViewModel()
            {
                FirstName = "walter",
                LastName = "white",
                Title = "cook"
            },
            new PartyViewModel()
            {
                FirstName = "jesse",
                LastName = "pinkman",
                Title = "lackey"
            }
        },
        ManagementParties = new PartyViewModel[]
        {
            new PartyViewModel()
            {
                FirstName = "Gus",
                LastName = "Freng",
                Title = "Boss"
            }
        },
        ConcealParties = new ConcealPartyViewModel[]
        {
            new ConcealPartyViewModel()
            {
                FirstName = "Skyler",
                LastName = "White",
                Title = "Wife",
                ConcealmentDescription = "money laundering"
            },
            new ConcealPartyViewModel()
            {
                FirstName = "Mike",
                LastName = "Ehrmantraut",
                Title = "Enforcer",
                ConcealmentDescription = "hid the bodies"
            }
        },

        GeneralDescription = "general description",
        DetaledDescription = "detailed description"
    };

    return View(vm);
}

[HttpPost, Route("report/new", Name = RouteNames.PostReport)]
public ActionResult FileReport(AddComplianceReportViewModel vm)
{
    return View(vm);
}
来自我的控制器的相关代码:

public class AddComplianceReportViewModel 
{
    public PartyViewModel[] InvolvedParties { get; set; }
    public PartyViewModel[] ManagementParties { get; set; }
    public ConcealPartyViewModel[] ConcealParties { get; set; }

    public string ReportedByFirstName { get; set; }
    public string ReportedByLastName { get; set; }
    public string ReportedByBestTimeToContact { get; set; }
    public bool ReportedByRemainAnonymous { get; set; }

    [Phone]
    public string ReportedByPhone { get; set; }

    [EmailAddress]
    public string ReportedByEmail { get; set; }

    public string GeneralDescription { get; set; }
    public string DetaledDescription { get; set; }
}

public class PartyViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
}

public class ConcealPartyViewModel : PartyViewModel
{
    public string ConcealmentDescription { get; set; }
}
[HttpGet, Route("report/new")]
public ViewResult FileReport()
{
    var vm = new AddComplianceReportViewModel()
    {
        ReportedByFirstName = "amy",
        ReportedByLastName = "lastname",
        ReportedByEmail = "my@email.com",
        ReportedByBestTimeToContact = "best time",
        ReportedByPhone = "555-123-4567",
        ReportedByRemainAnonymous = false,
        InvolvedParties = new []
        {
            new PartyViewModel()
            {
                FirstName = "saul",
                LastName = "goodman",
                Title = "lawyer"
            },
            new PartyViewModel()
            {
                FirstName = "walter",
                LastName = "white",
                Title = "cook"
            },
            new PartyViewModel()
            {
                FirstName = "jesse",
                LastName = "pinkman",
                Title = "lackey"
            }
        },
        ManagementParties = new PartyViewModel[]
        {
            new PartyViewModel()
            {
                FirstName = "Gus",
                LastName = "Freng",
                Title = "Boss"
            }
        },
        ConcealParties = new ConcealPartyViewModel[]
        {
            new ConcealPartyViewModel()
            {
                FirstName = "Skyler",
                LastName = "White",
                Title = "Wife",
                ConcealmentDescription = "money laundering"
            },
            new ConcealPartyViewModel()
            {
                FirstName = "Mike",
                LastName = "Ehrmantraut",
                Title = "Enforcer",
                ConcealmentDescription = "hid the bodies"
            }
        },

        GeneralDescription = "general description",
        DetaledDescription = "detailed description"
    };

    return View(vm);
}

[HttpPost, Route("report/new", Name = RouteNames.PostReport)]
public ActionResult FileReport(AddComplianceReportViewModel vm)
{
    return View(vm);
}
我不知道我是否需要分享这些观点。有很多视图代码需要缩减到相关的位。如果需要,我可以添加我的视图

这是我的浏览器发送到上述第二个控制器方法的表单数据:

ReportedByFirstName=amy&ReportedByLastName=latname&ReportedByPhone=%28555%29+123-4567&ReportedByEmail=my%40email.com&ReportedByBestTimeToContact=best+time&%5B0%5D.FirstName=saul&%5B0%5D.LastName=goodman&%5B0%5D.Title=律师&%5B1%5D.FirstName=walter&%5B1%5b%5D.LastName=white&%5B1%5b%5D.Title=cook&%5B2%5b%5D.FirstName=jesse&%5b%5b%5b%5D.pinkman%5b%5%5%2D.Title=仆人和%5B0%5D.FirstName=Gus和%5B0%5D.LastName=法国人和%5B0%5D.Title=老板和一般描述=一般+描述和持续时间=&AwarenessReason=&AwarenessReason=&AwarenessReason=&AwarenessReason=&AwarenessReason=&%5B0%5D.FirstName=Skyler和%5B0%5D.LastName=White和%5B0%5D.LastName=White和%5B0%5D.Title=妻子和%5B0%5b%5D.隐匿描述=洗钱和%5B1%5D.FirstName=Mike和%5B1%5B1er&%5B1%5D.隐藏描述=隐藏+主体

这是不可能读取的,所以这里它位于Fiddler提供的图像中(并非所有字段都适合该图像):

如下图所示,没有一个集合正在填充:

我需要做什么来填充这些?我不知所措。我注意到在Fiddler屏幕截图中,聚会的收集是模棱两可的。我如何告诉MVC“这群人是隐藏方,这群人是管理方”等等

============================================================================

\u Parties.cshtml

@model Redacted.Web.Models.Home.PartyViewModel[]

<div class="row">
    <div>@Html.Label("First Name")</div>
    <div>@Html.Label("Last Name")</div>
    <div>@Html.Label("Title")</div>
</div>

@for (var i = 0; i < Model.Count(); i++)
{
    <div class="row">
        <div class="columns medium-1">&nbsp;</div>
        <div class="columns medium-3">
            @Html.EditorFor(model => model[i].FirstName)
        </div>
        <div class="columns medium-3">
            @Html.EditorFor(model => model[i].LastName)
        </div>
        <div class="columns medium-3">
            @Html.EditorFor(model => model[i].Title)
        </div>
    </div>
}

ManagementParties
收集的方式相同。
隐藏方
集合具有不同的部分视图,但部分视图的加载方式相同。

您需要将部分视图发送到整个主模型,然后使用该模型访问字段。由于您没有这样做,字段的名称格式不正确,因此模型绑定无法完成这项工作

请参见此处(从您的表单数据):

这应该是:

InvolvedParties[0].FirstName
但是它不能以
InvolvedParties
作为前缀,因为您的局部视图认为顶级模型是列表本身,因此省略了
InvolvedParties

另一个技巧是防止模型中的
null
集合,只需使用coalesce在模型构造函数中初始化它们:

public AddComplianceReportViewModel ()
{
    InvolvedParties = InvolvedParties ?? new PartyViewModel[] { };
}

只需发布视图的一部分,在该部分中为一个集合(例如为)呈现输入ConcealedParties@Andrei,我已经用一些视图更新了问题。另外,如果你的应用程序有很多表单或浏览器/服务器交互,那么,使用KnockoutJS+JQuery或AnguarJS开发一个单页应用程序可能比使用回发更容易。我正在使用jQuery,只是为了让问题简短一点,没有包括它。我想我知道问题出在哪里了。。。在表单数据中,子项列为[0].FirstName,但它们不指定它们所属的属性。我想它们应该是“隐藏聚会[0].FirstName”之类的。我喜欢这个。这就是我要找的。但请看我问题的最后一部分。我正在把收集的资料交给分部。如果我传入整个模型,我将无法区分要在部分模型中使用的集合。这有意义吗?我仍然可以在每个集合中重复使用相同的部分吗?我接受这个。非常感谢你!我将我的
\u Parties.cshtml
视图复制到
\u ManagementParties.cshtml
,并让每个视图显示一个集合。再次感谢。@Amy没问题,很高兴我能帮上忙。