从ViewModel更新模型

时间:2017-08-10 18:47:19

标签: asp.net-mvc

我有一个表单,我从模型Store显示数据,但使用ViewModel(因为视图需要一些其他属性),称为StoreEmployee

表单的目标是更新Store的一些值。

当我在邮政行动中向商店控制器提交信息时,我有疑问。

这是信息:

My View正在使用此模型:

@model Application.Models.ApplicationviewModels.StoreEmployee

商店模式:

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

这是我的ViewModel

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

这是我目前的Post方法:

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

这是我知道如何更新模型的方法,但由于我将ViewModel(StoreEmployee)发送到PostMethod,因此无效。此刻是&#34;更新&#34;使用它自己的值而不从表单中获取数据。

如何从表单中捕获数据并进行更新?

我在想这样的事情可能是必要的

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

更新:这是视图

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

提前致谢!

0 个答案:

没有答案