2011-05-04 106 views
0

我想使用asp.net mvc实现一个产品。
我的产品分为几个模块,我想使用jquery tab小部件来指导用户通过提交。
我的ProductController将viewModel对象列表发送到产品视图。
所以我的产品视图看起来是这样的:通过jquery更新viewModels ajax

@model IList<View.Products.Modules.IModuleView> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#tabs").tabs({ ajaxOptions: 
      { 
       type: 'POST', 
       cache: false 
      } 
     }); 
    }); 
</script> 

<div id="tabs"> 
    <ul> 
     <li><a href="#fragment-1"><span>Tab1</span></a></li> 
     <li><a href="#fragment-2"><span>Tab2</span></a></li> 
     <li>@Html.ActionLink("Result","Result","Product")</li> 
    </ul> 
    <div id="fragment-1"> 
     @{ 
      var viewModelA = Model.Where(c => c.GetType() == typeof(WebMvcUI.Models.ModelA)).First(); 
      var viewModelB = Model.Where(c => c.GetType() == typeof(WebMvcUI.Models.ModelB)).First(); 
      var viewModelC = Model.Where(c => c.GetType() == typeof(WebMvcUI.Models.ModelC)).First(); 
     } 
     @Html.Partial("viewA", viewModelA) 
     @Html.Partial("viewB", viewModelB) 
     @Html.Partial("viewC", viewModelC) 
    </div> 
    <div id="fragment-2"> 
     Lorem ipsum dolor... 
    </div> 
</div> 

到目前为止,一切都很好。当用户点击最后一个选项卡时,他会在我的ProductController上调用操作Result。这里是我的问题:什么是收集我的部分视图的所有形式信息,发送回控制器并更新我的viewModels的最佳方式?

感谢您的任何建议!

回答

0

这是我的当前的解决方案:
(我的模块共用一个形式和演示对象被存储在会话)

jQuery的:

$('#tabs').bind('tabsselect', function (event, ui) { 
     var formToSubmit = $('form:first'); 
     var jqxhr = $.post(formToSubmit.attr('action'), formToSubmit.serialize(), 
      function ShowResult(data) { 
       $("#fragment-2").html(data); 
      } 
     ); 
    }); 

所述控制器:

[HttpPost] 
    public ActionResult Result(FormCollection form) 
    { 
     viewModelA = (ViewModelA)Presenter.Modules.Where(c => c.GetType() == typeof(WebMvcUI.Models.ViewModelA)).First(); 
     viewModelB = (ViewModelB)Presenter.Modules.Where(c => c.GetType() == typeof(WebMvcUI.Models.ViewModelB)).First(); 

     TryUpdateModel<ViewModelA>(viewModelA, form); 
     TryUpdateModel<ViewModelB>(viewModelB, form); 

     TransactionResult result = Presenter.CheckBusinessRules(true); 

     if (result.IsDirty) 
     { 
      return Content(result.Message); 
     } 

     return PartialView(); 
    }