-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicPolymorphicListController.cs
51 lines (48 loc) · 1.5 KB
/
DynamicPolymorphicListController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using PolymorphicModelBinder.Samples.Mvc.Models;
using PolymorphicModelBinder.Samples.Mvc.Models.Devices;
using PolymorphicModelBinder.Samples.Mvc.Models.Pets;
namespace PolymorphicModelBinder.Samples.Mvc.Controllers
{
public class DynamicPolymorphicListController : Controller
{
[HttpGet]
public IActionResult Index()
{
return View(new DynamicPolymorphicListViewModel()
{
Pets = new List<Pet>()
{
new Cat()
{
Name = "Garfield",
CanMeow = true
},
new Dog()
{
Name = "Noeska",
CanBark = true,
}
}
});
}
[HttpPost]
public IActionResult Index(DynamicPolymorphicListViewModel viewModel)
{
return View(viewModel);
}
[HttpPost]
public IActionResult AddEntry(DynamicPolymorphicListViewModel viewModel, string type)
{
viewModel.Pets.Add(type switch
{
"Dog" => new Dog() { Name = "New dog" },
"Cat" => new Cat() { Name = "New cat" },
_ => throw new ArgumentOutOfRangeException(nameof(type))
});
return PartialView(viewModel);
}
}
}