2010-09-24 74 views
0

這裏是JS:jQuery發佈到ASP.NET MVC2動作,字符串是綁定,整數不綁定...?

$('#createReview').click(function() { 
    CKEDITOR.instances['ReviewText'].updateElement(); 
    $.ajax({ 
     type: 'POST', 
     cache: false, 
     url: '/Review/Create', 
     data: $('#reviewForm').serialize(), 
     dataType: 'html', 
     success: function (response) { 
      $('#bookReview').html(response); 
     } 
    }); 
    return false; 
}); 

'createReview' 是

行動:

[HttpPost, ExportModelState] 
    public ActionResult Create(Review review) 
    { 
     if (ModelState.IsValid) 
     { 
      if (review.Create()) 
       return PartialView("EditReview", review); 
     } 

     return RedirectToAction("Edit"); 
    } 

當表單被提交,審查被創建,但只有字符串屬性是綁定的 - 在這種情況下是ReviewText。根本沒有一個整數屬性是綁定的。

最奇怪的部分是,當我在調試模式下運行它時,沒有的屬性成功綁定,甚至沒有ReviewText。當我檢查Review對象時,所有內容都是null或默認值。

我在普通模式和調試之間來回切換,它每次都做同樣的事情。

我不知所措。

編輯:

這裏是序列化()調用的全力輸出,這將不適合評論:

得分= 0 &得分= 0 &得分= 0 &得分= 0 &得分= 0 &得分= 0 &得分= 0 &得分= 0 &得分= 0 &得分= 0 & Book.Review.Rating = 0 &等級= 0 & ReviewID = 0 & ParentBookID = 1 & ReviewText =%3Cp%3E%0A%09I%26%2339%3Bm + an + idiot%3C%2Fp%3E%0A%3Cbr +%2F%3E%0A%3Cdiv + firebugversion% 3D%221.5.4%22 + ID%3D%22_firebugConsole%22 +風格%3D%22display%3A +無%3B%22%3E%0A%09%26nbsp%3B%3C%2Fdiv%3E%0A%3Cbr +% 2F%3E%0A & DateCreated = 1%2F1%2F0001 + 12%3A00%3A00 + AM

請注意,在我的整個數據庫中找不到「分數」,並且所有關於Firebug的垃圾都混在其中。

編輯#2:

好了,所有這些 「分數」 輸入從jQuery插件RATY,這是及時取消插件-ED的到來。

Firebug吸納了來自CKEditor實例的文本,它甚至在提交表單之前都沒有更新。

這個客戶端的東西肯定是爆炸!

哎呀...

+0

您是否嘗試過報警'$( '#reviewForm')的值。序列()'用於調試目的?結果是什麼? – 2010-09-24 23:57:23

+0

得分= 0&得分= 0&得分= 0&得分= 0&得分= 0&得分= 0&得分= 0&得分= 0&得分= 0&得分= 0&Book.Review.Rating = 0&評分= 0&ReviewID = 0&ParentBookID = 1&reviewText =%3CP%3E%0A%09This +書+是+可怕。 ..%3C%+%2F%3E%0A%09%26nbsp%3B%3C%2Fp%3E%0A&DateCreated = 1%2F1%2F0001 + 12%3A00%3A00 + AM – asfsadf 2010-09-25 00:07:27

回答

0

我要繼續前進,這粉筆一上來插件的一個糟糕的組合:螢火蟲,CKEditor的和Raty。

3

您還沒有表現出你的模型類看起來怎麼樣也不貴形式所包含的內容輸入元素。試圖重現這個問題是我創建的一個工作示例。它應該是非常接近您的方案:

型號:

public class Review 
{ 
    public int ReviewID { get; set; } 
    public int ParentBookID { get; set; } 
    public int Rating { get; set; } 
    public string ReviewText { get; set; } 
    public int[] Scores { get; set; } 
} 

控制器:

public class ReviewController : Controller 
{ 
    public ActionResult Edit() 
    { 
     var model = new Review 
     { 
      ReviewID = 1, 
      Rating = 5, 
      Scores = new[] { 1, 2, 3 } 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Create(Review review) 
    { 
     if (ModelState.IsValid) 
     { 
      if (review.Create()) 
      { 
       return PartialView("EditReview", review); 
      } 
     } 

     // Notice that redirecting in an AJAX invoked action will simply 
     // send an HTTP redirect to the Edit action and redisplay the whole page 
     // which is probably not what you are looking for. Maybe it would be better 
     // to return a partial here. 
     return RedirectToAction("Edit");   
    } 
} 

查看:

<% using (Html.BeginForm("Create", "Review", FormMethod.Post, new { id = "reviewForm" })) { %> 
    <div> 
     <%: Html.LabelFor(x => x.ReviewID)%> 
     <%: Html.TextBoxFor(x => x.ReviewID)%> 
    </div> 
    <div> 
     <%: Html.HiddenFor(x => x.ParentBookID)%> 
    </div> 
    <div> 
     <%: Html.LabelFor(x => x.Rating)%> 
     <%: Html.TextBoxFor(x => x.Rating)%> 
    </div> 
    <div> 
     <%: Html.LabelFor(x => x.ReviewText)%> 
     <%: Html.TextAreaFor(x => x.ReviewText)%> 
    </div> 
    <%: Html.EditorFor(x => x.Scores)%> 

    <input type="submit" value="Review" /> 
<% } %> 

<div id="bookReview"></div> 

腳本:

<script type="text/javascript" src="<%: Url.Content("~/scripts/jquery-1.4.1.min.js") %>"></script> 
<script type="text/javascript"> 
$(function() { 
    $('#reviewForm').submit(function() { 
     $.ajax({ 
      type: this.method, 
      url: this.action, 
      data: $(this).serialize(), 
      dataType: 'html', 
      success: function (response) { 
       $('#bookReview').html(response); 
      } 
     }); 
     return false; 
    }); 
}); 
</script> 

你也可以看看優秀jquery form plugin,使你的腳本簡化爲這樣:

$(function() { 
    $('#reviewForm').ajaxForm(function(response) { 
     $('#bookReview').html(response); 
    }); 
}); 
+0

Thanks Darin。我希望那裏有*一個名爲Score的數組 - 事實是,我用那個Serialize()調用發送了一大堆垃圾,我不知道它來自哪裏。我也看到一些關於Firebug的垃圾。我想知道是否與CKEditor,Firebug和jQuery的組合有關。 RedirectToAction來自我試圖實現P-R-G模式。那麼形式插件是GTG呢?那時候我想過那個。 – asfsadf 2010-09-25 19:20:09