1

I have a form where I show data from the model Store but using a ViewModel (since the view needs some other properties) called StoreEmployee.

The objective of the form is to update some values of the Store.

I have doubts when I submit the information to the Store Controller in the Post Action.

This is the info:

My View is using this model:

@model Application.Models.ApplicationviewModels.StoreEmployee

Store model:

public class Store
{
    [Key]
    public int StoreID { get; set; }
    [StringLength(60)]
    [Display(Name = "Nombre")]
    public string StoreName { get; set; }
    [Display(Name = "Área")]
    public int StoreArea { get; set; }
    public string IncomePeriodicity { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaDesignado { get; set; }
    public int Usuario { get; set; }

    public virtual LocalExpenses LocalExpenses { get; set; }        
    public virtual HechosLiquidador HechosLiquidadors { get; set; }

This is my ViewModel

    public class StoreEmployee
{
    public IEnumerable<Store> Stores { get; set; }
    public HechosLiquidador Hechos { get; set; }
}

And this is my Post Method at the moment:

[HttpPost, ActionName("Management")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Management(int? id, StoreEmployee StoreEmployee)
    {
        var storetoupdate = _context.Stores.SingleOrDefault(m => m.StoreID == id.Value);

        if (await TryUpdateModelAsync(
            storetoupdate,
            "",
            s => s.Usuario, s => s.IncomePeriodicity, s => s.FechaDesignado))
        {
            await _context.SaveChangesAsync();
            return RedirectToAction("Management");
        }

        return RedirectToAction("Management");
    }

This is the method I know on how to update a model but this is not working since I'm sending a ViewModel (StoreEmployee) to the PostMethod. At this moment is "updating" with it's own values and not taking the data from the form.

How can I capture the data from the form and update it?

I was thinking something like this might be necesary

            if (await TryUpdateModelAsync(
            storetoupdate,"Store",
            s => s.Usuario (?)

Update: This is the View

@model Application.Models.ApplicationviewModels.StoreEmployee
@using Application.Models
@{
ViewData["Title"] = "Management";

}
@Html.Partial("_NavBar")
<h2>Management</h2>
<hr />

<table class="table table-bordered table-hover" >
<tbody>
    <tr>
        //columns for titles
    </tr>
    @foreach (var item in Model.Stores)
    {
        <tr>
            <td hidden  >
                <form id="@(String.Format("{0}{1}","form",item.StoreID))" asp-action="Management" asp-route-id="@item.StoreID" method="post">
                </form>
            </td>
            <td class="col-md-3">
                <input form="@(String.Format("{0}{1}","form",item.StoreID))" type="hidden" asp-for="@item.StoreID" />
                <div class="form-group" form="@(String.Format("{0}{1}","form",item.StoreID))">
                    <div>
                        <input form="@(String.Format("{0}{1}","form",item.StoreID))" asp-for="@item.StoreName" name="StoreName" readonly class="form-control" />
                        <span asp-validation-for="@item.StoreName" class="text-danger"></span>
                    </div>
                </div>
            </td>
            <td class="col-md-2">
                <div class="form-group" form="@(String.Format("{0}{1}","form",item.StoreID))">
                    <div>
                        <select asp-for="@item.Usuario" name="Usuario" class="form-control" asp-items="ViewBag.UserId" form="@(String.Format("{0}{1}","form",item.StoreID))"></select>
                        <span asp-validation-for="@item.Usuario" class="text-danger"></span>
                    </div>
                </div>
            </td>
            <td>
                <div class="form-group" form="@(String.Format("{0}{1}","form",item.StoreID))">
                    <div>
                        <input type="text" class="datepicker form-control" asp-for="@item.FechaDesignado" name="FechaDesignado" id="@(String.Format("{0}{1}","ownID",item.StoreID))" form="@(String.Format("{0}{1}","form",item.StoreID))" />
                        <span asp-validation-for="@item.FechaDesignado" class="text-danger"></span>
                    </div>
                </div>
            </td>
            <td class="col-md-2">
                    <div class="form-group" form="@(String.Format("{0}{1}","form",item.StoreID))">
                        <div>
                            <select name="Status" asp-for="@item.IncomePeriodicity" class="form-control" form="@(String.Format("{0}{1}","form",item.StoreID))">
                                <option value="0">Diario</option>
                                <option value="1">Semanal</option>
                                <option value="2">Mensual</option>
                            </select>
                            <span asp-validation-for="@item.IncomePeriodicity" class="text-danger"></span>
                        </div>
                    </div>
            </td>
            <td class="col-md-1">
                    <input form="@(String.Format("{0}{1}","form",item.StoreID))" id="submit-data" type="submit" value="Update" class="btn btn-default" />
            </td>
        </tr>}
</tbody>

Thanks in advance!

0 Answers0