2010-04-01 23 views
0

我有一個觀點,我想單擊下一個按鈕時填充數據。它是3個視圖,它將在每個下一個按鈕上發送數據。我該怎麼做呢?提交數據到一個查看MVC的數據表

下面是代碼,我只是做了,但應該給的一個想法是什麼我尋找...

第1頁:

<table> 
    <tr> 
     <td><b>Name:</b></td> 
     <td colspan="2"><input id="txtName" type="text" /></td>       
    </tr> 
    </table> 
    <input type="submit" value="Next" /> 

第2頁:

<table> 
    <tr> 
     <td><b>Address:</b></td> 
     <td colspan="2"><input id="txtAddress" type="text" /></td>       
    </tr> 
    </table> 
    <input type="submit" value="Next" /> 

第3頁:

<table> 
    <tr> 
     <td><b>Phone:</b></td> 
     <td colspan="2"><input id="txtPhone" type="text" /></td>       
    </tr> 
    </table> 
    <input type="submit" value="Next" /> 
+0

你想實現什麼是所謂的'wizard'。我建議你爲此搜索。這肯定是一個重複的問題。 – Omar

回答

0

我想通了。在使用存儲過程插入值之前,我在輸入中使用了name屬性,然後存儲在對象中。

<td colspan="2"><input name="Name" id="txtName" type="text" /></td> 

模型

public string Name {get; set;} 
//Constructor 
using (connection) 
{ 
    SqlCommand command = new SqlCommand("StoredProcName", connection); 
    command.Parameters.Add(new SqlParameter("name", Name)); 
    connection.Open(); 
    command.ExecuteNonQuery(); 
} 
0

在y我們認爲,你需要一個表格。該動作是您要調用的特定控制器上的操作。提交後,該操作會在帖子上獲取表單收集,這是您的表單中的數據所在的位置。獲取數據併爲數據庫對象類型創建實體。然後將該對象插入到數據庫中。假設你將實現Linq到Sql或類似的東西。

MyView的

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %> 
<table> 
    <tr> 
     <td><b>Name:</b></td> 
     <td colspan="2"><input id="txtName" type="text" /></td> 
    </tr> 
</table> 
<input type="submit" value="Next" /> 
<% Html.EndForm(); %> 

myController的

[AcceptVerbs(HttpVerbs.Post)] 
public virtual ActionResult MyAction(FormCollection collection) 
{ 
    // take your data from your collection and populate entities to save into your db 
    return RedirectToAction("Page2"); // or whatever the action is you want to redirect to 
}