I have to do dropdown list with names of plates and due to selected name show all subtypes of that plate. This looks like this in code:
Country Class:
public class Country
{
public List<Plate> plates { get; set; }
public string name { get; set; }
public Country(string jsonRead)
{
plates = JsonConvert.DeserializeObject<List<Plate>>(jsonRead);
}
public IEnumerable<Plate> getAllPlates()
{
return plates.OrderBy(r => r.type);
}
}
Plate class:
public class Plate
{
public string type { get; set; }
public List<Subtype> subtypes { get; set; }
public Plate()
{
subtypes = new List<Subtype>();
}
}
Subtype class:
public class Subtype
{
public string subtypeName { get; set; }
public string color { get; set; }
public Subtype(string subtype, string color)
{
this.subtypeName = subtype;
this.color = color;
}
}
In the controller, I have a field CountryData. It has information about all tables (I read it from file) and function to view where I want to have these 2 dropdownlists:
[Route("Home/CreatePlate/{name}")]
public IActionResult CreatePlate(string name)
{
var model = countryData.GetCountry(name);
return View(model);
}
And finally CreatePlate.cshtml view:
@model Generator.Country
@{
ViewData["Title"] = "CreatePlate";
}
<h1>CreatePlate</h1>
<div>
<h4>Country</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
<select id="Plate" name="Plate" onchange="this.form.submit()">
@if (Model.plates != null)
{
foreach (var Title in Model.plates)
{
<option value="@Title.type">@Title.type</option>
}
}
</select>
<select id="Subtype" name="Subtype" onchange="this.form.submit()">
@if (Model.plates != null)
{
foreach (var Title in Model.plates.Select(r => r))
{
foreach (var subtype in Title.subtypes)
{
<option value="@subtype.subtypeName">@subtype.subtypeName</option>
}
}
}
</select>
</dt>
<dd class = "col-sm-10">
</dd>
</dl>
</div>
For now I show all plates in first foreach, but in the second, I get ALL subtypes instead of that selected in first dropdownlist.
I saw a few solutions on Stackoverflow, but all of them need JS (I don't know that lang) and also are little different from my problem.
That's my 1st project in ASP.NET Core MVC.