2013-07-19 49 views
0

我在.net MVC4是新的和我有一些問題....保留數據從(多)文本框

例:鑑於我有2個文本框

<input type="text" name="tax1" style="width:20px" maxlength="1" /> 
<input type="text" name="tax2" style="width:60px" maxlength="4" /> 

後,我推提交按鈕我想保留來自文本框的兩個數據。

Ex : string value = textbox1 + textbox2 

我可以按照此示例(在視圖中)來執行我的要求嗎?

如果確定:請告訴我有關解決方案。

如果不行:請告訴我有關解決方案和解決方案(ex.controller等)的文件。

+0

你想保持這個文本框值在控制器? –

+0

我可以在View中保留這個值嗎? –

+0

你想把你的頁面發佈到'controller',或者你只是把這個'textbox'值保存在你的'view'頁面中。 – Jaimin

回答

0

你有類似於您查看以下表格:

@using(Html.BeginForm()) 
{ 
    <input type="text" name="tax1" style="width:20px" maxlength="1" /> 
    <input type="text" name="tax2" style="width:60px" maxlength="4" /> 
    <input type="submit" value="Submit" /> 
} 

在你的控制器:

public ActionResult SomeAction(string tax1, string tax2) 
{ 
    string newString = tax1 + tax2; 
} 
0

有幾個方法可以做到這一點。一種方式是mostruash。我通常將我的觀點綁定到view model。我從來不以任何其他方式去做。我從不使用屬性或域模型,只使用視圖模型。我會告訴你如何。

您的視圖模型看起來是這樣的:

public class SomeViewModel 
{ 
    public string Tax1 { get; set; } 

    public string Tax2 { get; set; } 
} 

然後在你的操作方法,你需要將它傳遞給你的觀點:

public ActionResult SomeAction() 
{ 
    SomeViewModel viewModel = new SomeViewModel(); 

    return View(viewModel); 
} 

而且在您的文章操作方法,你需要接收此視圖模型作爲輸入參數:

[HttpPost] 
public ActionResult SomeAction(SomeViewModel viewModel) 
{ 
    // Check for null viewModel 

    if (!ModelState.IsValid) 
    { 
      return View(viewModel); 
    } 

    // Do what ever else you need to do 
} 

然後在您的視圖上:

@model SomeProject.ViewModels.Servers.SomeViewModel 

@Html.TextBoxFor(x => x.Tax1) 
@Html.TextBoxFor(x => x.Tax2) 

我希望這有助於。