LoginSignup
9
6

More than 5 years have passed since last update.

ASP.NET MVC ViewからControllerへ値を渡す方法

Last updated at Posted at 2016-06-27

自分のメモ代わりですが、Qiitaに投稿してみたくて書いてみました。
この記事では以下の環境を使っています。
・Visual Studio 2015
・ASP.NET MVC5

viewからcontorllerへ値を渡す方法

以下の様なViewがあるとします。

@for(int i = 0; i < length; i++)
{
    <input type="text" name="data1" value="@Model.entryData[i]" />
    <input type="text" name="data2" value="@Model.entryData[i]" />
}

※上記の記載はformタグで囲まれています。

この値をControllerに渡したいとすると、controller側のアクションの引数を以下の様にすればOKです。

public ActionResult Action1(string[] name1, string[] name2)
{
    中身・・・
}

ポイントは、Viewのinputタグのnameの値と、Controllerのアクションの引数名を一致させる点です。
これで自動的に紐づけをしてくれます。

Controllerへ渡す値がmodelの場合や、
modelのlistの場合なども、自分で使った後に追記していきたいと思います。

以下の記事を参考にさせて頂きました。ありがとうございました。
http://my-clip-devdiary.blogspot.jp/2009/10/viewcontroller.html

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6