2012-12-05 21 views
2

我有一個測試框控件和一個提交按鈕控件,與我的表單一起使用標籤控件。當我在我的testbox控件中鍵入一些文本並單擊按鈕控件時,我無法在我的標籤控件中顯示該文本。請幫助。在MVC3中單擊提交按鈕後顯示標籤中的測試箱文本剃鬚刀

因爲我是MVC的新手,我正在學習基礎知識,如果您能夠給我全面的編碼,對我非常有幫助。

你也可以寄給我編碼。提前致謝。

我的代碼如下:

型號

using System; 
using System.Collections.Generic; 
using System.Linq; 

using System.Web; 

namespace TestMVCProj.Models 

{ 
    public class TestPost 
    { 
     public string name {get; set;} 
    } 
} 

控制器

using TestMVCProj.Models 

public ActionResult TestPost(TestPost tp) 
{ 
    return View(); 
} 

查看

@model TestMVCProj.Models.TestPost 
@{ 
    ViewBag.Title ="TestPost"; 
} 
@using (Html.BeginForm()) 
{ 
    @Html.TestBoxFor (x => x.name) 
    <input type="submit" /> 
} 
Your Name is <%= Model.name %> 

回答

0

我認爲你需要通過模型時後查看。

public ActionResult TestPost(TestPost tp) 
{ 
    return View(tp); 
} 
0

你可能需要在控制器的兩個動作:

  1. 即需要HTTP GET,沒有參數 - 初始頁面加載
  2. 即需要HTTP POST,模型作爲參數 - 用於回傳

public ActionResult TestPost() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult TestPost(TestPost post) 
{ 
    return View(post); 
} 

此外,由於您使用的剃刀查看,語法應該是這樣的:

Your Name is @Model.name 
0

更改視圖,

@model TestMVCProj.Models.TestPost 
@{ 
    ViewBag.Title ="TestPost"; 
} 
@using (Html.BeginForm()) 
{ 
    Html.TextBoxFor (x => x.name) 
    <input type="submit" /> 
} 
Your Name is @Model.name