私はちょうどC#の学習を始め、簡単なプロジェクトを作成しようとしました。最初に私はテキストボックスを作成し、誰かが名前を入力すると、その名前は次のページに表示されるはずですが、何が間違っているのか分かりません。テキストボックスと送信ボタンが表示されます。テキストボックスに名前を入力して送信をクリックすると、次のページに表示されません。あなたの入力」フィールドのname属性の値はSearchString
あるシンプルなC#とMVCプロジェクト
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Newproject.Models;
namespace Newproject.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Welcome(FormCollection form, string CustNo)
{
Session["SearchString"] = form["CustNo"];
ViewBag.Name = Session["SearchString"];
return View();
}
}
}
Index.cshtml
@{
ViewBag.Title = "Access TextBox and TextboxFor Value From View To Controller In Asp.net MVC";
}
<br />
@Html.Label("Name")
<br />
@using (Html.BeginForm("Welcome","Home",FormMethod.Post))
{
@Html.TextBox("SearchString", "", new { @class = "CustNo" });
<br/>
<input id="btnSubmit" name="btnSubmit" placeholder="test" type="submit" onclick = "location.href='@Url.Action("Welcome", "Home", new {})'" />
<br/>
}
Welcome.cshtml
@{
ViewBag.Title = "Welcome";
string CustNo = ViewBag.Name;
}
<p> @CustNo</p>
セッション["SearchString"] =フォーム["CustNo"]; 'でブレークポイントを設定し、その値がフォームからどのようなものか確認しましたか?これらは基本的なデバッグスキルです。また、ViewBagに頼るのではなく、MVCの "Model"部分を使用する必要があります。 – mason