所以我目前正在学习ASP.net MVC的工作,而且我只是在玩它来学习一些东西。目前,我有一个网页,其中提供了来自C#代码(模拟数据库)的硬编码列表和一个显示菜单的创建按钮,允许我为新人添加信息,然后单击创建按钮在底部添加它。但是,创建按钮当前不起作用。如何创建一个方法来获取该输入并将其添加到列表中,此外,该方法应该在哪里? PersonController? PersonModel?谢谢你的帮助!
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")
}
答案 0 :(得分:1)
public ActionResult Create(){}
[HttpPost]
public ActionResult Create(PersonModel model)
{
people.Add(new PersonModel {Age = model.Age, Name = model.Name, Title =model.Title});
return View();
}
答案 1 :(得分:1)
网站是基于请求的 ....所以您要将其添加到的列表不能存在于您的控制器中,因为控制器已创建e ach time请求控制器中的操作。
这实际上会重置您的列表。
您需要创建一个可从任何地方访问的静态列表....
答案 2 :(得分:0)
您需要做的是在Controller中调用一个方法来填充列表。 一目了然,您的 EditorFors 应该没问题,并正确填充您的模型。
使用
@using (Html.BeginForm("ActionMethod","Controller",FormMethod.Post))
会调用您的ActionMethod,您应该接受 PersonModel 。
public ActionResult ActionMethod(PerosnModel vm)...
在那里,您可以使用提交的模型执行所需操作。 (即将其添加到db表,发布,保存或显示)。
希望这有帮助。