2013-05-16 56 views
0

我想在asp.net MVC查詢控件和results.Aka發佈控件值和顯示結果在同一頁上的一個很好的例子。查詢和結果在同一頁

我谷歌在這一點,但無法找到任何例子

+3

這是一個巨大的問題,完全依賴於任何其他因素。你想不同步嗎?模型如何「複雜」,以及「所有返回數據」有多少數據?從這裏開始:http://www.asp.net/mvc,回來一個更具體的問題。 –

+0

謝謝克里斯,我簡化了我的問題。 – ron

回答

0

我會什麼,我認爲你把裂縫是後,這是具有形狀+結果在同一頁上。我最近遇到了這個問題,我需要這種模式,並且增加了兩種方法(表單和表單+結果)所需的複雜性。不是GET和POST。以下是我做的:

控制器方法:

// GET: /Reports/Refund 
public ActionResult Refunds() 
{ 
    var refundVM = new RefundCheckReportViewModel(); 
    return View(refundVM); 
} 

// GET: /Reports/Refund 
public ActionResult RefundsResult(RefundCheckReportViewModel refundVM) 
{ 
    string errorMsg; 
    var ready = refundVM.IsReadyToRun(out errorMsg); 

    if (!ready) 
     ModelState.AddModelError("StartDate", errorMsg); 
    else 
     refundVM.LoadReportData(); // this method on the ViewModel fetches the report data. 

    return View("Refunds", refundVM); 
} 

查看在剃刀,其持有的形式,再加上表中報告的結果:

@model MembershipCenter.ViewModels.Report.RefundCheckReportViewModel 

@{ 
    ViewBag.Title = "Refunds"; 
} 

<div class="formStandard"> 


    @using (Html.BeginForm("RefundsResult", "Reports", FormMethod.Get)) 
    { 

     <div class="fsHeader"> 
      <span class="reqLabel"><a class="req">*</a>required field</span> 
      <strong>Refund Check Report</strong> 
     </div> 

     <div class="fsBlock"> 
      <div class="text">Date Range: <a class="req">*</a></div> 
      <div class="elem"> 
       <span class="dateRangePicker"> 
        From: @Html.TextBoxFor(model => model.StartDate, new { @class = "short datepicker" }) 
        &nbsp;&nbsp;&nbsp;&nbsp; 
        To: @Html.TextBoxFor(model => model.EndDate, new { @class = "short datepicker" }) 
        @Html.ValidationMessageFor(model => model.StartDate) 
       </span> 
      </div> 
     </div> 



     <div class="fsFinalActions"> 
      <input type="submit" value="Generate Report" /> 
     </div> 
    } 

</div> <!-- end .formStandard --> 



@if (Model.ReportData != null) 
{ 
    if (Model.ReportData.Any()) { 
     <table class="colorTable" style="margin-top: 30px;" id="resultTable"> 
      <thead> 
       <tr> 
        <th>Date</th> 
        <th>Member #</th> 
        <th>Amount</th> 
        <th>Check #</th> 
        <th>Name</th> 
        <th>Reason</th> 
       </tr> 
      </thead> 
      <tbody> 
       @foreach (MembershipCenter.ViewModels.Report.RefundCheckReportViewModel.RefundReportResultItem item in Model.ReportData) 
       { 
        <tr> 
         <td>@item.PrintedDate</td> 
         <td>@Html.GetMemberLink(item.MemberNumber)</td> 
         <td>@item.Amount</td> 
         <td>@item.CheckNumber</td> 
         <td>@item.PayeeName</td> 
         <td>@item.Reason</td> 
        </tr> 
       } 
      </tbody> 
     </table> 
    } 
    else { 
     <div><strong>There are not Refunds for the criteria above.</strong></div> 
    } 
} 

最後,僅供參考,我的視圖模型:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Membership = Biz.Business.Membership; 
using Printing = Biz.Business.Printing; 

namespace MembershipCenter.ViewModels.Report 
{ 
    public class RefundCheckReportViewModel 
    { 
     [Required] 
     public DateTime? StartDate { get; set; } 
     [Required] 
     public DateTime? EndDate { get; set; } 

     public List<RefundReportResultItem> ReportData = null; 

     public RefundCheckReportViewModel() 
     { 
     } 

     public bool IsReadyToRun(out string errorMessage) 
     { 
      errorMessage = string.Empty; 

      if (!StartDate.HasValue || !EndDate.HasValue) 
       errorMessage = "Please enter a start and end date"; 

      else if (StartDate.Value <= new DateTime(2012, 11, 1).Date) 
       errorMessage = "Please enter a date greater than 11/1/2012 for the Start Date."; 

      return (errorMessage.IsNullOrEmpty()); 
     } 

     public void LoadReportData() 
     { 
      ReportData = new List<RefundReportResultItem>(); 

      var refunds = Membership.Getrefunds(blah); 
      var checks = Printing.GetChecks(blah); 

      foreach (var refund in refunds) 
      { 
       var check = checks.SingleOrDefault(c => c.RequestReferenceId == refund.Request.Id); 
       if (check != null) 
        ReportData.Add(new RefundReportResultItem(refund, check)); 
      } 
     } 

     public class RefundReportResultItem 
     { 
      public int CheckNumber { get; set; } 
      public decimal Amount { get; set; } 
      public string PayeeName { get; set; } 
      public string MemberNumber { get; set; } 
      public string Reason { get; set; } 
      public string PrintedDate { get; set; } 

      public RefundReportResultItem(Membership.Refund refund, Printing.PrintedCheck check) 
      { 
       MemberNumber = refund.MemberId; 
       Reason = refund.Note; 
       Amount = refund.Amount; 

       PayeeName = check.Payee; 
       CheckNumber = check.CheckNumber; 
       PrintedDate = check.QueuedToPrint.ToShortDateString(); 
      } 
     } 
    } 
} 

這視圖模型下RefundReportResultItem類是人C對於報告中每個結果項目的數據都不重要。

+0

感謝格雷厄姆,讓ViewModel包含請求參數並使其效果非常好! – ron

+0

是的,我發現「胖視圖模型,瘦控制器,愚蠢的看法」的風格真的很有幫助。 ViewModel應該回答關於它自己的所有問題,並公開控制器或視圖需要完成其工作的任何內容,但僅限於此。我真的很喜歡從控制器內部,你可以這樣做:'返回View(「Refunds」,refundVM);',它可以讓你綁定請求到不同的View,同時傳遞一個Model。 – Graham