I´m having an issue where I have three models as show below: Person, Competence with PersonCompetence between them. My current controller method gets an Id from previous page and shows that person with a list of this person's Competence and Level. In this View however I want to have a POST for new Competence. So at the same time you are adding a new one and you can see which ones you already have.
With the controller method I have now I can access the PersonCompetence and Competence when showing the list.
I dont have access to the Competence properties for asp-for="Competence" marked ###### in the View for AddComp.
- I need the IDof person for POST to right person
- I need the CompetenceTypefor POST to that property
- I need PersonCompetenceto show the list of currentPersonCompetence.
I get that with the current @model CompetenceRadar.Models.Person I only reach Person properties.
I have looked at having a ViewModel with access to all tables with an IEnumerable for each table, but this breaks my current Controller when I search for the Id of the person showing. I have switched the @model in the View, but then I can't access Person ID/name.
So how do I access the Competence properties , list one person and get a list of PersonCompetences for that Person.
Please tell me if you want me to clarify something. I don't need working code, just something to point me in the right direction for a solution.
- Is it a ViewModel?
- Can I POST without the asp-forattribute?
Models
public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public ICollection<PersonCompetences> PersonCompetences { get; set; }
}
public class PersonCompetence
{
    public int ID { get; set; }
    public int PersonID { get; set; }         // FK
    public int CompetenceID { get; set; }         // FK
    public int Level { get; set; }
    public Competece Competence { get; set; }
    public Person Person { get; set; }
}
public class Competence
{
    public int ID { get; set; }
    public string CompetenceType { get; set; }
    public string CompetenceCategory { get; set; }
    public ICollection<PersonCompetence> PersonCompetences { get; set; }
}
AddComp Kontroller function
public async Task<IActionResult> AddComp(int? id)
{
    var person = await _context.Personer
        .Include(pk => pk.PersonCompetences)
        .ThenInclude(k => k.Competence)
        .FirstOrDefaultAsync(m => m.ID == id);
    return View(person);
}
View_AddComp View for AddComp
@model CompetenceRadar.Models.Person
<h1>AddComp</h1>
<div class="row">
    <form asp-action="AddComp">
        <input type="hidden" asp-for="ID" />
        <div class="form-group col-sm-4">
            <label asp-for="#############" class="control-label col-sm-4"></label>
            <input asp-for="#############" class="form-control col-sm-4" />
            <span class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Save" class="btn btn-dark" />
        </div>
    </form>
</div>
@foreach (var item in Model.PersonCompetences)
{
    <div class="row py-2 rounded" style="background-color:lightslategray">
        <div class="col-sm-3 pt-2">@item.Competence.CompetenceType</div>
        <div class="col-sm-1 pt-2">@item.Niva</div>
        <div class="col-sm-3 pt-2">@item.Competence.CompetenceCategory</div>
        <div class="col-sm-5 d-flex justify-content-end">
            <a class="btn btn-dark mr-1" role="button" asp-area="" asp-controller="Competence" asp-action="UpdateLevel" asp-route-id="@item.ID">Update</a>
            <a class="btn btn-dark mr-1" role="button" asp-area="" asp-controller="Competence" asp-action="DeleteComp" asp-route-id="@item.CompetenceID">Remove</a>
        </div>
    </div>
}
