ビューのGETリクエストで 'plate'オブジェクトをインスタンス化し、フォームがポストされるときにアクセスします。プレートオブジェクトには、フォームからのデータが入力されます。また、渡された座標を格納するために使用できますか?あるいは私が達成しようとしていることをする慣用的な方法は何ですか?ASP.NET MVC 5:コントローラの永続変数
ありがとうございます! HTTPはステートレスであるため、あなたが見る何
public class DrawingController : Controller
{
Plate plate;
[HttpGet]
public ActionResult Index()
{
plate = new Plate()
{
Holes = new Holes()
}
return View(plate);
}
// called by ajax when user clicks on "Save" to save the input coordinates
[HttpPost]
public void PassCoordinates(string coordinates)
{
// why is plate null here?
plate.Holes.coordinates = coordinates;
}
[HttpPost]
public ActionResult Index(Plate plate)
{
// I want to access plate.Holes.coordinates that I set in 'PassCoordinates'
// how can I achieve this?
}
}
静的変数として 'plate'を設定することができます。しかしそれでも、あなたはここで深刻な競争状態の問題を起こすことになる。通常、値を格納するとは、何らかのデータベースまたはキャッシュ機構を介して永続化することを意味します。 – nurdyguy
なぜ2つのポストメソッドがありますか? Index.cshtmlを提供できますか? – 0014
@ 0014、PassCoordinates(文字列座標) 'がajaxによって呼び出され、' Index(Plate plate) 'がフォーム送信時に呼び出されます。 – user3453636