0

Let's say I have a class like this

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime BirthDate { get; set; }
    public List<Work> Employments { get; set; }
}

public class Work
{
    public string Employer { get; set; }
    public int Salary { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

My controller have both HttpGet and httpPost for Edit

[HttpGet]
public IActionResult Edit(int id)
{
    var _item = new Person()
    {
        Id = id,
        Name = "Bob",
        BirthDate = new DateTime(1970, 3, 4),
        Employments = new List<Work>()
        {
            new Work(){ Employer="Microsoft", Salary=5000, StartDate= new DateTime(1998, 1, 1), EndDate=new DateTime(2005, 12, 31)},
            new Work(){ Employer="Google", Salary=6000, StartDate= new DateTime(2006, 1, 1), EndDate=new DateTime(2013, 12, 31)},
            new Work(){ Employer="Spotify", Salary=10000, StartDate= new DateTime(2014, 1, 1)}
        }
    };
    return View(_item);
}

[HttpPost]
public IActionResult Edit(Person _item)
{
    return View(_item);
}

and my Edit.csthml looks like

@Model  Person

<form asp-action="Edit">
    <div class="form-group">
        <label asp-for="Name" class="control-label"></label>
        <input type="string" asp-for="Name" class="form-control" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="BirthDate" class="control-label"></label>
        <input type="date" asp-for="BirthDate" class="form-control" />
        <span asp-validation-for="BirthDate" class="text-danger"></span>
    </div>

    <GRID>
        table with the rows of Work
    </GRID>
    <button type="submit" class="btn-primary">Send</button>
</form>

I don't know how to get the Grid part, all example I see are the grid standalone or have own AJAX request, not part of the other properties.

In old asp.net WebForms you could have it in one update.

So how do you do things like this in .NET Core?

The question is NOT part of the update in database, I use Dapper and stored procedures, not EF Core...

The question is how to retrieve the edited data to my generic class before DB-update...

1
  • why do you need a grid? you can retrieve the data to a table Commented Jun 14, 2020 at 10:49

1 Answer 1

2

Do you want to update person and corresponding work data on the edit view?

First of all, there is no Grid tag in core, you can use the original table tag to display the table.

And you also need to put the Id key field of the Person class into the form, and use the hidden control to hide it to ensure that the value of the Id field can also be passed.

To ensure that all updated data can be passed to the controller, you need to add the name attribute to the control corresponding to the work class and ensure that they are in the form of Employments[index].FieldName.

More details, please refer to the following example:

@model Person
@{
    var i = 0;
}
<form asp-action="Edit">
    <input type="hidden" asp-for="Id" class="form-control" />
    <div class="form-group">
        <label asp-for="Name" class="control-label"></label>
        <input type="text" asp-for="Name" class="form-control" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="BirthDate" class="control-label"></label>
        <input type="date" asp-for="BirthDate" class="form-control" />
        <span asp-validation-for="BirthDate" class="text-danger"></span>
    </div>
    <div class="form-group">
        <table>
            <tr>
                <th>Employer</th>
                <th>Salary</th>
                <th>StartDate</th>
                <th>EndDate</th>
            </tr>
            @foreach (var item in Model.Employments)
            {
                <tr>
                    <td>
                        <input type="text" asp-for="@item.Employer" name="Employments[@i].Employer" class="form-control" />
                        <span asp-validation-for="@item.Employer" class="text-danger"></span>
                    </td>
                    <td>
                        <input type="number" asp-for="@item.Salary" name="Employments[@i].Salary" class="form-control" />
                        <span asp-validation-for="@item.Salary" class="text-danger"></span>
                    </td>
                    <td>
                        <input type="date" asp-for="@item.StartDate" name="Employments[@i].StartDate" class="form-control" />
                        <span asp-validation-for="@item.StartDate" class="text-danger"></span>
                    </td>
                    <td>
                        <input type="date" asp-for="@item.EndDate" name="Employments[@i].EndDate" class="form-control" />
                        <span asp-validation-for="@item.EndDate" class="text-danger"></span>
                    </td>
                </tr>
                i++;
            }
        </table>
    </div>
    <button type="submit" class="btn-primary">Send</button>
</form>

Here is the test result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

YES! Thanks, that was exact what was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.