2010-07-27 50 views
2

我想使用jqModal創建一個簡單的表單來添加新產品。
jqModal和ASP.NET MVC

查看/首頁/的Index.aspx:

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

      $('#addProductControlSection').jqm({ modal: true, 
       ajax: '<%: Url.Action("AddProduct", "Home") %>', 
       onHide: myAddClose 
      }); 

      function myAddClose(hash) { 
       hash.w.fadeOut('1000', function() { hash.o.remove(); }); 
      } 

     }); 
    </script> 

    // rest of the code... 

<a href="#" class="jqModal">Add product</a> 

<div id="addProductControlSection" class="jqmWindow"> 

</div> 

的HomeController:

public ActionResult AddProduct() 
{ 
    return View(); 
} 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult AddProduct(Product product) 
{ 
    if(!ModelState.IsValid) 
    { 
     // how to show an error? 
    } 

    _productRepository.Save(product); 
    // how to display 'success' or something... 
} 

我不知道如何實現驗證。如果用戶爲Product.Price輸入了不正確的值並單擊保存按鈕,我不想關閉表單。我想在正常視圖上使用驗證摘要時顯示錯誤消息。

謝謝!

回答

0

看看jQuery validation plugin或使用MVC Model Validation來自動生成JS。它在模態對話中的事實應該對這些技術沒有影響。

+0

當我在模態對話框上單擊Save按鈕時,我輸入if(!ModelState.IsValid)並返回View(「AddProduct」,product)被調用。之後,模式對話框關閉,我想阻止它。 – 2010-07-27 14:17:53

+0

關於提交按鈕...我忘了包含'MicrosoftAjax.js'和'MicrosoftMvcAjax.js'腳本。 – 2010-07-28 13:26:00