2013-06-05 37 views
0

Modal Daialog如何通過JSON

提交一個模態對話框與Diactionaries視圖模型我想提出一個模式對話框,回到頁面上的結果模式消失後。

我的視圖模型----

public class InsertPoStep3 
    { 
    public Dictionary<int, string> FixedCategoryList { get; set; } 
    public string Description { get; set; } 
    public Dictionary<int, string> FixedDateRangeList { get; set; } 
    } 

該視圖模型將與字典到控制器的ActionResult提交如果我按提交按鈕

控制器的ActionResult ------

public ActionResult CreateCategory(InsertPoStep3 insertPoStep3) 
    { 
     // foreach (var data in formCollection) 
     // { 

     var fixedCategory = insertPoStep3.FixedCategoryList.Values; 
     //var description = formCollection[formCollection.Count - 1]; 
     var category = new Category 
     { 
      //CategoryId = _db.Categories.Count() + 1, 
      //CategoryDescription = description, 
      FixedCategoryId = Convert.ToInt32(fixedCategory), 

     }; 
     //_db.Categories.Add(category); 
     // _db.SaveChanges(); 

     /* for(int i=1;i<formCollection.Count-1;i++) 
     { 
      _db.DateRanges.Add(new DateRange 
            { 
             CategoryId = category.CategoryId, 
             FixedDateRangeId =  Convert.ToInt32(formCollection[i]) 

            }); 
     } 


     _db.SaveChanges();*/ 

     return Json(insertPoStep3); 
    } 

有太多評論...實際上我不知道如何從視圖模型與字典中獲取數據....

我的模態對話框的JavaScript -------

<script type="text/javascript"> 
$(function() { 


    $("#InsertCategoryAnchor").click(function() { 

     $("#form-dialog").load("/InsertPo/CreateCategory"); 
     $("#form-dialog").dialog({ 
      modal: true, 

      height:500, 


      width: 700, 
      closeOnEscape: true, 
      resizable: false, 
      draggable: true, 

      title: "Insert a Category", 

      close: function(){ 
       $(this).dialog("destroy"); 
      }, 



      buttons: { 
       Submit: function() { 
        var value = $("#myfrom").serialize(); 

        $.ajax({ 
         url: "/InsertPo/CreateCategory", 
         type: "POST", 
         data: value, 
         success: function (student) { 
          alert("Yes"); 

          $(this).dialog('close'); 
         } 
        }); 

       }, 
       Cancel: function() { 
        $(this).dialog("destroy"); 
       } 
      } 
     }); 
    }); 
    }); 

    </script> 

如何實際代碼將???請任何人一步一步回答...我在一個修復....代碼和scenerios在這裏...但它不工作.... JSon和字典...

+0

hey Sujit .......告訴我這些代碼的輸出成功:function(student){ alert(student); –

+0

陷入錯誤...不正確執行成功 – user1882264

+0

讓我看看post方法的代碼。 –

回答

0

你'重新做這件事太困難了。 ASP.NET MVC具有內置的Ajax功能。你想要做的是讓你的主頁上的按鈕鏈接到部分視圖。如果用戶點擊一個按鈕,將會彈出一個模式來加載你的局部視圖。在部分視圖中,使用Ajax.BeginForm()來處理回發到服務器。它真的很簡單。我知道,因爲我剛剛創建了一堆使用模態彈出窗口來插入和更新視圖模型的頁面。