2012-01-12 29 views
2

我目前有一個3步向導。動態添加EditorTemplates到當前頁面,在下拉更改

第1步:需要一些細節我需要並具有下拉,你選擇包

第2步的數量:這取決於你有多少包選擇的這一步 顯示您所需確認您的訂單

我:編輯軟件包

步驟3號被告知我需要結合步驟1 & 2,但我對如何使用mvc和剃鬚刀做到這一點不知所措,因爲它僅僅是您的型號的一個視圖...

這樣做的最佳方法是什麼?

它是否會通過專門指定需要多少包的操作將該頁面提交回自己的情況,或者這可以通過ajax完成嗎?

感謝

回答

2

我的方法是如下:

爲包行創建一個部分(PackageRow.cshtml)

@model IEnumerable<PackageViewModel> 
<div class="editorRow"> 
@using (Html.BeginCollectionItem("packages")) 
{ 
    @Html.TextBoxFor(x => x.WhateverYouNeed) 
} 
</div> 

載入你的包行到通過形式ajax對用戶選擇行數

@Html.ActionLink("Go!", "AddPackages", null, new { id = "addPackages" }) 

<div id="editorRows"></div> 

<script type="text/javascript"> 
$(document).ready(function() { 
     $("#addPackages").click(function() { 
      $.ajax({ url: this.href, cache: false, success: function (html) { 
       $("#editorRows").append(html); // add the number of rows you need here 
      } 
      }); return false; 
     }); 
     $("a.deleteRow").live("click", function() { $(this).parents("div.editorRow:first").remove(); return false; }); 
    }); 
</script> 

通過控制器

public ActionResult AddPackages() 
{ 
    return PartialView("PackageRow", new PackageViewModel { ... }); 
} 

保存加入您的泛音到表單中的數據

[Authorize] 
[HttpPost] 
     public ActionResult CreatePackages(int id, FormCollection fc) 
     { 

      int nrKeys = fc.AllKeys.Count(); 
      int i = 0; 

      int interations = (nrKeys/2); 

      foreach (var key in fc.AllKeys) 
      { 

       if (nrKeys <= i) 
        break; 

       if (i != 0) 
       { 
        string value1 = fc[i]; 
        string value2 = fc[i + 1]; 
        ... 
       } 
       else 
       { 
        i++; 
       } 

      ... 
+0

謝謝,我給這個一去 – JamesStuddart 2012-01-12 15:50:15

+0

只是修正了幾個錯別字你。 – Nick 2012-01-12 16:00:40

+0

謝謝,這表明我正確的方向,我不能發佈我的確切解決方案,因爲它在整個項目中的分解。但這是一個很好的起點。再次感謝 – JamesStuddart 2012-01-13 15:45:18

相關問題