gpt4 book ai didi

c# - 在 html mvc C# 中访问多个 ViewModel

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:30 24 4
gpt4 key购买 nike

我有一个包含两个 View 模型的 View 模型类

public class SalesByBrandParentVM
{
public IEnumerable<SPSalesbyBrandModel> SpSalesByBrandModel { get; set;}

public ReportSalesbyBrandVM ReportSalesByBrandModel { get; set; }
}

我的观点是这样的:

@model MyStoreReports.ViewModels.SalesByBrandParentVM

@{
ViewBag.Title = "Sales by Brand Page";
}
@section Styles {
<link href="@Url.Content("~/css/report.css")" rel="stylesheet" type="text/css" />
}


<div class="row" >
<div class="col-lg-2">


<form method="post">
<h2>Sales by Brand</h2>

<input asp-for="FromDate" class="form-control" type="date" />
<div class="form-group">
<label asp-for="FromDate">FromDate</label>
<span asp-validation-for="FromDate" class="text-muted"></span>
</div>

<input asp-for="ToDate" class="form-control"type="date"/>
<div class="form-group">
<label asp-for="ToDate">ToDate</label>
<span asp-validation-for="ToDate" class="text-muted"></span>
</div>

<div class="form-group">
<a asp-controller="App" asp-action="Index" class="btn btn-default">Cancel</a>
<input type="submit" value="Submit" class="btn btn-success" />
</div>
</form>
</div>
</div>



<div>
<table>
<tr>
<th>ClientCode</th>
<th>TrxAmount</th>
</tr>
@foreach (var item in Model.SpSalesByBrandModel)
{
<tr>
<td>@item.ClientCode</td>
<td>@item.TrxAmount</td>
</tr>
}
</table>
</div>

public class SPSalesbyBrandModel
{
[Key]
public string ClientCode { get; set; }

public double TrxAmount { get; set; }

}

public class ReportSalesbyBrandVM
{
[Required]
public DateTime FromDate { get; set; }

[Required]
public DateTime ToDate { get; set; }

}
[HttpPost]
public IActionResult ReportSalesbyBrand(ReportSalesbyBrandVM viewmodel)
{
var fdate = viewmodel.FromDate;
var edate = viewmodel.ToDate;
var category = viewmodel.Category;
return View(_totalSalesRepository.GetSalesbyBrand(fdate,edate,category));
}

在表格内,我可以指向 SpSalesByBrandModel 模型类。有没有办法将 FromDate 和 ToDate 控件指向 ReportSalesByBrandModel viewmodel

提前致谢!!!

最佳答案

简单使用ReportSalesByBrandModel.ToDate:

<label asp-for="@Model.ReportSalesByBrandModel.ToDate">ToDate</label>

因为 ToDateReportSalesByBrandModel 属性的属性

关于c# - 在 html mvc C# 中访问多个 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43028743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com