私は現在、自分の仕事のためにASP.net MVCを学習しています。現在、C#コード(データベースをシミュレートする)のハードコードされたリストと、メニューを表示するCreateボタンがあり、新しい人物の情報を追加してから、作成ボタンをクリックすることができるWebページがありますそれを追加するには下部にあります。ただし、作成ボタンは現在動作しません。その入力を受け取り、それをリストに追加するメソッドを作成するにはどうすればよいのですか。 PersonController? PersonModel?助けてくれてありがとう!ウェブサイトから入力してC#リストに追加する
namespace ASPpractice.Controllers
{
public class PersonController : Controller
{
List<PersonModel> people = new List<PersonModel>();
// GET: Person
public ActionResult Index()
{
return View();
}
public ActionResult Person()
{
people.Add(new PersonModel {Age = 76, Name = "Rick", Title = "Drunk Mad Scientist"});
people.Add(new PersonModel {Age = 13, Name = "Morty", Title = "Tool of a Grandson"});
people.Add(new PersonModel {Age = 35, Name = "Jerry", Title = "Unemployed Dumbass Father"});
return View(people);
}
public ActionResult Create()
{
return View();
}
}
}
namespace ASPpractice.Models
{
public class PersonModel
{
private string _Name;
private string _Title;
private int _Age;
public string Name { get { return _Name; } set { this._Name = value; }}
public string Title { get { return _Title; } set { this._Title = value; } }
public int Age { get { return _Age; } set { this._Age = value; } }
}
}
@model ASPpractice.Models.PersonModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PersonModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
は}
<div>
@Html.ActionLink("Back to List", "Person")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
それは本当にASP.NET MVC上のいくつかのチュートリアルから利益を得ることができるあなたの学習経験のように聞こえます。例:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started本質的には、2つの 'Create'アクションがあります。ユーザーはオブジェクトを作成するための値を入力するためのビューを表示し、もう一方はそれらの値のフォームポストを受け入れ、新しく作成されたオブジェクトをデータベースに保存します。保存すると、ユーザーを再度インデックス/リストビューにリダイレクトして、すべてのオブジェクトを再度表示します。 – David
ああ、このチュートリアルは私が必要としたものかもしれないようです。私はmvcモデルのためのmsdnに関するいくつかの文書を読みましたが、何とか私はこの問題を遭遇しませんでした。ありがとうございました。 –