2017-03-26 52 views
1

我有包含兩個的ViewModels訪問多個視圖模型

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控件ReportSalesByBrandBodeModel viewmodel

在此先感謝!

+0

如果您共享的ViewModels也將是有益的屬性。 –

+0

用viewmodel和控制器方法更新了問題 – Maria

回答

1

簡單使用ReportSalesByBrandModel.ToDate

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

由於ToDateReportSalesByBrandModel財產

+0

是的,它工作時,我設置{<標籤[email protected]> ToDate} – Maria

+0

當我這樣做,在控制器中,我得到viewmodel的不同實例設置爲null。 – Maria

+0

工作當我在控制器中傳遞父模型類而不是ReportSalesByBrandVM – Maria