模型绑定相关实体

时间:2017-07-07 13:04:51

标签: c# asp.net null model-binding aspnetboilerplate

我正在尝试ASP.NET Boilerplate框架,我非常喜欢它,但仍有很多东西需要学习。我不能使以下案例有效。 我有两个相关的实体:GuestSocialMedia

CreateGuestViewModel看起来像这样:

public class CreateGuestViewModel
{
    public CreateSocialViewModel SocialMedia { get; set; }
}

View看起来像这样:

<div class="form-group">
    <label asp-for="SocialMedia.Twitter"></label>
    <input asp-for="SocialMedia.Twitter" class="form-control" placeholder="Enter guest's twitter'" />
</div>

点击“保存”按钮后,在GuestAppService.Create方法中,SocialMedia成员为null

[AutoMapTo(typeof(Guest))]
public class CreateGuestInput
{
    public CreateSocialMediaInput SocialMedia { get; set; }
}

[AutoMapTo(typeof(SocialMedia))]
public class CreateSocialMediaInput
{
    public string Twitter { get; set; }
}

我确信我错过了什么。你觉得怎么样?

谢谢, 的Marius

1 个答案:

答案 0 :(得分:0)

在html方面,您使用CreateGuestViewModelCreateSocialViewModel作为模型。但是在应用程序服务上,您绑定了CreateGuestInputCreateSocialMediaInput。模型必须相同。