2011-12-18 77 views
2

我正在使用http://afana.me/post/create-wizard-in-aspnet-mvc-3.aspx嚮導在ASP.NET MVC3和多HttpPost

它的偉大工程中描述的嚮導控制,但我需要有多個HttpPost相同的控制器內。在我的場景中,我需要在轉到下一步之前添加到集合中。在該步驟的部分視圖中。我有以下設置:

@using (Html.BeginForm("AddJobExperience", "Candidate")) 
{ 
    <input type="submit" value="Add Experience" /> 
} 

當我點擊Add經驗輸入時,它被路由到

[HttpPost, ActionName("Index")] 
public ActionResult Index(CandidateViewModel viewModel) 
{ 
} 

代替

[HttpPost, ActionName("AddJobExperience")] 
public ActionResult AddJobExperience(CandidateViewModel col) 
{ 

} 

我究竟做錯了什麼?

+0

你不需要你的'[ActionName]'s。 – SLaks 2011-12-18 00:06:11

+0

我意識到問題不是[ActionName],但事實上我有一個Http.BeginForm()中的Http.BeginForm()。但問題依然存在。如何做一個郵政回來添加到收藏。 – jmogera 2011-12-18 00:10:50

+0

我沒有回答這個問題;我指出了一個冗餘。 – SLaks 2011-12-18 00:12:36

回答

1

這聽起來像是你需要將你的CandidateViewModel分解成單獨的ViewModel和你的大的Wizard視圖到不同的視圖中,這樣每個步驟對於嚮導都有一個動作。

第一步他們添加了工作經驗,所以有一個視圖,視圖模型和一個動作,第二步他們做任何其他的事情,你也有一個單獨的視圖,視圖模型和動作。等等

把你的CandidateViewModel分解成獨立的ViewModel將意味着你可以只關注該步驟所需的數據,並且可以添加驗證,然後當它們單擊提交時,它會將數據發佈到下一步。

然後,當你想提高的UI行爲,添加一些AJAX,也許使用的東西像jQuery UI選項卡,使其更像一個桌面應用程序的嚮導。

+0

亂七八糟後..我終於用這種方法,它工作得很好。沒有花哨的jquery! – jmogera 2012-01-18 16:07:54

1

這聽起來像你還是有嵌套形式。不要這樣做,它不是有效的HTML。

你有2個選擇這裏,取決於你想要達到的目的。如果你想分開發布你的工作經歷,那麼把它們放在自己的@using(Html.BeginForm中,但不要將它們嵌套在外面。當用戶點擊添加體驗按鈕時,請完成您的工作,然後將新視圖返回給用戶。

如果你想同時發佈所有的工作經驗,然後把他們全部包裝在一個@using(Html.BeginForm,不要把@using(Html.BeginForm)放在你的部分視圖中。請參閱another question I answered here for more info on how to post a collection of items in a single HTTP POST

第二種方法就像你試圖實現的一樣,爲此,你可能應該使用AJAX向你的集合添加多個工作體驗,而不需要做一個完整的回發。提交集合中的所有工作經驗給你的嚮導控制器,實現這樣的功能並不難:

Given I can see the Add Experience button 
When I click the Add Experience button 
Then I should see a new experience partial view with input fields 
And I should enter data in these fields 

When I click the Add Experience button a second time 
Then I should see another new experience partial view with input fields 
And I should enter data in these fields 

When I click the Add Experience button a third time 
Then I should see a third new experience partial view with input fields 
And I should enter data in these fields 

When I click the Next button in the wizard 
Then my controller will receive data for all 3 experiences I submitted in a single form 
1

你需要使用ActionMethodSelectorAttributeActionNameSelectorAttribute允許對行動增加新的屬性來調用相應的按鈕的不同作用點擊 在View:

@using (Html.BeginForm()) 
{ 
    <input type="submit" value="Add Experience" name="AddExperience" /> 
    <input type="submit" value="Add Experience" name="AddJobExperience" /> 
} 

在應用程序中添加新的類FormValueRequiredAttribute這擴展ActionMethodSelectorAttribute類以檢查哪個按鈕被點擊

//controller/FormValueRequiredAttribute.cs 

public class FormValueRequiredAttribute : ActionMethodSelectorAttribute 
    { 
     public string ButtonName { get; set; } 

     public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) 
     { 
      var req = controllerContext.RequestContext.HttpContext.Request; 
      return !string.IsNullOrEmpty(req.Form[this.ButtonName]); 
     } 

    } 

那麼你應該行動添加此屬性調用相應的動作

在控制器

[HttpPost] 
[FormValueRequired(ButtonName = "AddExperience")] 
public ActionResult Index(CandidateViewModel viewModel) 
{ 
    return View(); 
} 

[HttpPost] 
[ActionName("Index")] 
[FormValueRequired(ButtonName = "AddJobExperience")] 
public ActionResult AddJobExperience_Index(CandidateViewModel viewModel) 
{ 
    return View(); 
} 

注意如果您Index.cshtml方法Html.BeginForm那麼你不需要指定的索引操作ActionName屬性,現在AddJobExperience_Index的行爲與索引操作相同。