У меня есть модели, которые имеют отношение один ко многим. >
Код: Выделить всё
// Principal (parent)
public class Blog
{
public int Id { get; set; }
public int Name { get; set; }
public ICollection Posts { get; set;}
}
// Dependent (child)
public class Post
{
public int Id { get; set; }
public int Title { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Код: Выделить всё
public class CreateBlogVM
{
public int Name { get; set; }
public ICollection Posts { get; set;}
}
Код: Выделить всё
[HttpGet]
public ActionResult Create()
{
var vm = new CreateBlogVM()
{
Posts = [new Posts { Title = "" }]
};
return View(vm);
}
Код: Выделить всё
@model CreateBlogVM
Name:
@foreach (var post in @Model.Posts)
{
Title:
}
Код: Выделить всё
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create(CreateBlogVM model)
{
System.Console.WriteLine($"{string.Join("-", model.Posts)}");
Подробнее здесь: [url]https://stackoverflow.com/questions/79116580/is-there-a-ruby-on-rails-form-for-equivalent-on-net-mvc[/url]