Я создал новый проект и скопировал ваш код в проект. Я тоже получил нулевую модель при попытке создать. Потребовалось несколько изменений, чтобы исправить это для меня. Мне пришлось добавить Model.AccountOrOU в качестве переданного в модель к частичному. Мне пришлось инициализировать модель в контроллере для создания индекса. Мне также пришлось изменить атрибут имени в свойствах модели AccountOrOU в представлении. Вот мой код ->
Index.cs html
@model AccountVM
@{
ViewData["Title"] = "Home Page";
}
<h2>Create</h2>
@using (Html.BeginForm("SaveAccount", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div>
@{await Html.RenderPartialAsync("_Account.cshtml", Model.AccountOrOU);}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" />
</div>
</div>
</div>
}
_Account.cs html
@model AccountOrOU
<div class="form-horizontal" id="ViewData">
<h4>Account Partial</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" name="AccountOrOU.Name" class="form-control" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Модель
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Test2.Models
{
public class AccountVM
{
public AccountVM()
{
AccountOrOU = new AccountOrOU();
}
public AccountOrOU AccountOrOU { get; set; }
}
public class AccountOrOU
{
public string Name { get; set; }
}
}
Контроллер
public IActionResult Index()
{
AccountVM blah = new AccountVM();
return View(blah);
}
public IActionResult SaveAccount(AccountVM input)
{
return View("Index", input);
}