0

クラスがあり、インスタンスを作成してインデックスにプロパティを入力しますが、ビューで送信ボタンを押して再びインデックス アクションに戻ると、クラスのプロパティが null になります。

アクションに戻ってデータを取得するときに、データを保存するにはどうすればよいですか? 出来ますか?

viewbagインデックスと塗りつぶしのテーマでandを使用しviewdataましたが、再びインデックス アクションに戻ると、すべてのテーマが null でした :(

public class myclass
{
    public string tp { get; set; }
}

public class HomeController : Controller
{
    //
    // GET: /Home/
    myclass myc = new myclass();
    public ActionResult Index()
    {
        myc.tp = "abc";
        return View(myc);
    }
}

意見:

 @model MvcApplication2.Controllers.myclass
 @{
  Layout = null;
  }
<!DOCTYPE html>
<html>
 <head>
     <title>Index</title>
 </head>
 <body>

     @using (Html.BeginForm())
     {
        <input id="Submit1" type="submit" value="submit" />
     }

 </body>
</html>
4

2 に答える 2

0

次のようなフォームメソッドに従って、コントローラーでGETまたはPOSTメソッドのいずれかを使用するだけです。

[HttpGet]
public ActionResult Index(string id)
{
    myc.tp = id;
    return View(myc);
}
于 2012-06-07T08:32:16.053 に答える
0

ビューでプロパティの入力フィールドを指定した場合、HttpPost でモデルを取得してそのプロパティを確認できます。

[HttpPost]
public ActionResult Index(myclass myc)
{
    //check the myc properties here
    return View(myc);
}

次に、ビューで:

 @using (Html.BeginForm())
 {
@Html.TextBoxFor(m => m.tp)
于 2012-06-07T12:48:00.000 に答える