2010-07-27 53 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