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

标签 c# asp.net-mvc razor

我有一个包含两个 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/

相关文章:

c# - 具有 Equals 和 GetHashCode 辅助方法的库,适用于 .NET

c# - 类 PerformanceCounter 文档

asp.net-mvc - 如果 Roles 为空,则使 AuthorizeAttribute 默认拒绝用户

c# - 将模型从局部 View 传递到 Controller

asp.net-mvc - 如何使无序列表出现在 MVC3 Razor 的 foreach 循环中?

jquery - 根据下拉列表选择显示表单元素

c# - Xamarin.Forms:订阅事件处理程序 VS。指挥

c# - 为什么编译器允许将 Convert.ToString() 分配给整数?

asp.net - 在 ASP.NET MVC2 中运行耗时任务的可靠方法是什么

c# - Itextsharp 合并 Pdfs 与 acrofields