在html mvc C#中访问多个ViewModel

时间:2017-03-26 12:32:53

标签: c# asp.net-mvc razor

我有一个包含两个viewmodel的viewmodel类

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

提前致谢!!!

1 个答案:

答案 0 :(得分:1)

简单使用ReportSalesByBrandModel.ToDate

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

由于ToDateReportSalesByBrandModel属性的属性