2014-09-04 11 views
-1

我有註冊表單和用於查看所有記錄的表格(表格).. 但我不知道如何從不同的操作方法顯示 我的代碼顯示下面如何調用其他操作方法並在我的視圖中創建列表

@using (Html.BeginForm("Create", "Bathrooms", FormMethod.Post)) 
    { 

    @Html.LabelFor(model => model.BathRoomDetails) 
    @Html.TextBoxFor(model => model.BathRoomDetails) 
    <br/> 
     @Html.LabelFor(model => model.shortstring) 
    @Html.TextBoxFor(model => model.shortstring) 
    <br/> 
    <button type="submit" class="btn btn-primary"/> Create</button> 
    <button type="submit"> View All</button> 

} 

和我的格子碼

<table> 
    @foreach (var a in ViewBag.data as List<RealEstate.Models.BathRoomVM>) 
      { 
      <tr> 
       <td>@a.BathRoomDetails</td> 
       <td>@a.BActive</td> 
      </tr> 
      } 
</table> 

和我的控制器

public ActionResult Create() 
     { 

      return View(); 
     } 
    [HttpPost] 
     public ActionResult Create(BathRoomVM Bathrooms) 
     { 
      ModelState.Clear(); 
      var realContext = new RealEstateDBContext(); 
      Bathroom Bathroomobj = new Bathroom(); 
      Bathroomobj.BathRoomDetails = Bathrooms.BathRoomDetails; 
      Bathroomobj.shortstring = Bathrooms.shortstring; 

      realContext.Bathrooms.Add(Bathroomobj); 
      realContext.SaveChanges(); 
      ModelState.Clear(); 
      return View(); 
     } 

     public ActionResult Get() 
     { 
      var realContext = new RealEstateDBContext(); 
      var rslt = (from bathroom in realContext.Bathrooms 
         select new BathRoomVM { BathRoomDetails = bathroom.BathRoomDetails, BActive = bathroom.BActive, Bcancelled = bathroom.Bcancelled }).Where(m => m.BActive == true && m.Bcancelled == false).ToList(); 
      ViewBag.data = rslt; 
      return View();   

     } 

我需要加載或顯示網格後,單擊ViewAll按鈕..我已經嘗試了不同的方式,但他們都沒有工作..請幫助我如何實現,我從最近2天卡住了這個問題.. 我有使用

public ActionResult Create() 
     { 
      var realContext = new RealEstateDBContext(); 
      var rslt = (from bathroom in realContext.Bathrooms 
         select new BathRoomVM { BathRoomDetails = bathroom.BathRoomDetails, BActive = bathroom.BActive, Bcancelled = bathroom.Bcancelled }).Where(m => m.BActive == true && m.Bcancelled == false).ToList(); 
      ViewBag.data = rslt; 
      return View(); 
     } 

我以這種方式得到的結果,(需要更多的時間從數據庫中檢索數據)過慢..

[我需要調用其他的操作方法,並在我看來,創建一個列表]

回答

0

你真的需要擺脫使用viewbag。這是一個明顯的跡象,表明你不適合使用視圖模型。這就是說,你需要使用jQuery,視圖模型和局部視圖。「

這裏是件: 1.替換你的表格區域是一個div,然後將保存你的表,將從部分視圖返回。

  1. 創建一個包含您的表的邏輯和用途,這也可以從您的局部視圖控制器操作返回視圖模型的局部視圖.cshtml文件。

  2. 您需要爲提交按鈕指定一個阻止默認操作的單擊事件,以防止它提交頁面,而是調用局部視圖控制器操作更新表區域。

如果我有一定的清晰度,我可能會多一點幫助。當你點擊創建時,你想讓它更新表格還是僅當點擊查看全部時才顯示?

一些其他的想法。您可以使用jQuery在每次創建時在表中添加新行,然後使用jQuery ajax調用將浴室數據發送到服務器。

另一個可能有用的功能是使用MemoryCache。實現起來相當簡單,但每次添加新浴室時都需要重新設置,否則您將無法獲得更新列表。

如果您需要特定的東西,請告訴我。

+0

感謝朋友我明白了... – 2014-09-05 16:29:36

相關問題