Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to Update Model via ViewModel

$
0
0

Hello,

I have a view that is constructed used a ViewModel. It's an attempt of an editable grid. 

What this view constructs is a iteration of forms for each item inside the IEnumerable of Stores, each one with a submit button.

When I submit a form, it goes to the Post Action where I try to update the values of the object. However, I have a problem accesing this data since the model used by the view is a viewmodel.

Here is the ViewModel:

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

This is the view:

@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>}

And this is the Post Action:

[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");
}

I've read that I must add a prefix on this part:

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

Adding the name of the model it is related inside the ViewModel, but is not saving the changes.

I appreciate any help.

Thanks

 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>