在没有数据库的情况下创建表单并绑定到 class
Creating a form and binding to a class without a datatabase
我刚开始学习 MVC,确实需要一些帮助 - 因为我正在尝试从 ASP.NET Web 表单继续学习。我的所有数据库事务都有一个自定义 Web API,非常高效和可靠。我已经在 .NET Core 中对其进行了重新编码。
我的问题是我发现大多数与 MVC 相关的模型绑定示例都由 Entity Framework 个示例组成。我正在寻找帮助,展示如何使用 get() post(form) 操作将 ViewModel link 传递给控制器。我需要了解如何绑定到单选按钮列表等...
我正在使用下面的 class,它删除了数据库连接以简化 answers/suggestions。
public class BuildSearch
{
//Bootstrap date entry
public DateTime StartDate { get; set; }
//Bootstrap date entry
public DateTime EndDate { get; set; }
//Need this bound to a radio button list
public List<GeoArea> GeoAreas { get; set; }
public BuildSearch()
{
GeoAreas = new List<GeoArea>();
// StartDate = DateTime.Parse(DateTime.Now.AddDays(-31).ToShortDateString());
// EndDate = DateTime.Parse(DateTime.Now.ToShortDateString());
GeoAreas.Add(new GeoArea { GeoAreaItem = "Region", Id = 0 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager1", Id = 1 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager2", Id = 2 });
}
}
public class GeoArea
{
public int Id { get; set; }
public string GeoAreaItem { get; set; }
}
我正在尝试创建一个视图,它将显示这些数据,然后允许我 post 返回用户编辑。我有意让示例保持简单,因为一旦我弄清楚如何 post 返回,使用更新后的数据我就可以处理传递到网络 API 以完成我需要的工作完毕。只是沮丧地试图弄清楚我如何绑定到这种类型的 class。
这是一个单选按钮示例:
https://www.c-sharpcorner.com/article/radiobutton-in-asp-net-mvc/
这里是将表单数据从 html 表单导入控制器的一个很好的例子。
将数据放入视图模型中,您将执行如下操作
public async Task<IActionResult> Index()
{
var movies = await _context.Movies.ToList();
if (movies == null)
{
return NotFound();
}
return View(movies);
}
然后您将需要表格对您执行 post 操作 操作例如
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
if (ModelState.IsValid)
{
//post to api here
return RedirectToAction(nameof(Index));
}
return View(movie);
}
您必须将模型或视图模型传递到 html class as
@model MvcMovie.Models.Movie
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Movie</h4> etc etc
对于单选按钮,我会在您的 BuildSearch class 中添加一个 属性,名为 GeoAreaId。单选按钮选择将模型绑定到 post 上的此 属性。因此,您的 BuildSearch class 变为
public class BuildSearch
{
//Bootstrap date entry
public DateTime StartDate { get; set; }
//Bootstrap date entry
public DateTime EndDate { get; set; }
public int GeoAreaId { get; set; } //added field
//Need this bound to a radio button list
public List<GeoArea> GeoAreas { get; set; }
public BuildSearch()
{
GeoAreas = new List<GeoArea>();
// StartDate = DateTime.Parse(DateTime.Now.AddDays(-31).ToShortDateString());
// EndDate = DateTime.Parse(DateTime.Now.ToShortDateString());
GeoAreas.Add(new GeoArea { GeoAreaItem = "Region", Id = 0 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager1", Id = 1 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager2", Id = 2 });
}
public class GeoArea
{
public int Id { get; set; }
public string GeoAreaItem { get; set; }
}
}
你的控制器中的 get 方法看起来像这样
public IActionResult Search()
{
var buildSearch = new BuildSearch();
return View(buildSearch);
}
您的视图需要看起来像这样
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model BuildSearch
<form asp-controller="Home" asp-action="Search" method="post">
<label asp-for="StartDate">Start date</label>
<input asp-for="StartDate" />
<label asp-for="EndDate">End date</label>
<input asp-for="EndDate" />
<fieldset>
<legend>
GeoArea
</legend>
@foreach (var item in Model.GeoAreas)
{
<input type="radio" name="GeoAreaId" value="@item.Id" />
@item.GeoAreaItem
}
</fieldset>
<input type="submit" />
</form>
对于单选按钮,请注意名称属性如何匹配我添加到您的 BuildSearch class 的新 属性 GeoAreaId。这对于模型绑定的工作很重要。
然后您的控制器中的 post 方法需要如下所示
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Search(BuildSearch buildSearch)
{
//...
}
要查看发生了什么,请在此方法中设置一个断点。 运行 代码,在表单中输入一些值,然后单击提交。当代码在内部停止时,将鼠标悬停在 buildSearch 上,您将看到模型绑定已生效。 StartDate、EndDate 和 GeoAreaId 属性将包含您需要从表单中获取的值。
我刚开始学习 MVC,确实需要一些帮助 - 因为我正在尝试从 ASP.NET Web 表单继续学习。我的所有数据库事务都有一个自定义 Web API,非常高效和可靠。我已经在 .NET Core 中对其进行了重新编码。
我的问题是我发现大多数与 MVC 相关的模型绑定示例都由 Entity Framework 个示例组成。我正在寻找帮助,展示如何使用 get() post(form) 操作将 ViewModel link 传递给控制器。我需要了解如何绑定到单选按钮列表等...
我正在使用下面的 class,它删除了数据库连接以简化 answers/suggestions。
public class BuildSearch
{
//Bootstrap date entry
public DateTime StartDate { get; set; }
//Bootstrap date entry
public DateTime EndDate { get; set; }
//Need this bound to a radio button list
public List<GeoArea> GeoAreas { get; set; }
public BuildSearch()
{
GeoAreas = new List<GeoArea>();
// StartDate = DateTime.Parse(DateTime.Now.AddDays(-31).ToShortDateString());
// EndDate = DateTime.Parse(DateTime.Now.ToShortDateString());
GeoAreas.Add(new GeoArea { GeoAreaItem = "Region", Id = 0 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager1", Id = 1 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager2", Id = 2 });
}
}
public class GeoArea
{
public int Id { get; set; }
public string GeoAreaItem { get; set; }
}
我正在尝试创建一个视图,它将显示这些数据,然后允许我 post 返回用户编辑。我有意让示例保持简单,因为一旦我弄清楚如何 post 返回,使用更新后的数据我就可以处理传递到网络 API 以完成我需要的工作完毕。只是沮丧地试图弄清楚我如何绑定到这种类型的 class。
这是一个单选按钮示例: https://www.c-sharpcorner.com/article/radiobutton-in-asp-net-mvc/
这里是将表单数据从 html 表单导入控制器的一个很好的例子。
将数据放入视图模型中,您将执行如下操作
public async Task<IActionResult> Index()
{
var movies = await _context.Movies.ToList();
if (movies == null)
{
return NotFound();
}
return View(movies);
}
然后您将需要表格对您执行 post 操作 操作例如
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
if (ModelState.IsValid)
{
//post to api here
return RedirectToAction(nameof(Index));
}
return View(movie);
}
您必须将模型或视图模型传递到 html class as
@model MvcMovie.Models.Movie
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Movie</h4> etc etc
对于单选按钮,我会在您的 BuildSearch class 中添加一个 属性,名为 GeoAreaId。单选按钮选择将模型绑定到 post 上的此 属性。因此,您的 BuildSearch class 变为
public class BuildSearch
{
//Bootstrap date entry
public DateTime StartDate { get; set; }
//Bootstrap date entry
public DateTime EndDate { get; set; }
public int GeoAreaId { get; set; } //added field
//Need this bound to a radio button list
public List<GeoArea> GeoAreas { get; set; }
public BuildSearch()
{
GeoAreas = new List<GeoArea>();
// StartDate = DateTime.Parse(DateTime.Now.AddDays(-31).ToShortDateString());
// EndDate = DateTime.Parse(DateTime.Now.ToShortDateString());
GeoAreas.Add(new GeoArea { GeoAreaItem = "Region", Id = 0 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager1", Id = 1 });
GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager2", Id = 2 });
}
public class GeoArea
{
public int Id { get; set; }
public string GeoAreaItem { get; set; }
}
}
你的控制器中的 get 方法看起来像这样
public IActionResult Search()
{
var buildSearch = new BuildSearch();
return View(buildSearch);
}
您的视图需要看起来像这样
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model BuildSearch
<form asp-controller="Home" asp-action="Search" method="post">
<label asp-for="StartDate">Start date</label>
<input asp-for="StartDate" />
<label asp-for="EndDate">End date</label>
<input asp-for="EndDate" />
<fieldset>
<legend>
GeoArea
</legend>
@foreach (var item in Model.GeoAreas)
{
<input type="radio" name="GeoAreaId" value="@item.Id" />
@item.GeoAreaItem
}
</fieldset>
<input type="submit" />
</form>
对于单选按钮,请注意名称属性如何匹配我添加到您的 BuildSearch class 的新 属性 GeoAreaId。这对于模型绑定的工作很重要。
然后您的控制器中的 post 方法需要如下所示
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Search(BuildSearch buildSearch)
{
//...
}
要查看发生了什么,请在此方法中设置一个断点。 运行 代码,在表单中输入一些值,然后单击提交。当代码在内部停止时,将鼠标悬停在 buildSearch 上,您将看到模型绑定已生效。 StartDate、EndDate 和 GeoAreaId 属性将包含您需要从表单中获取的值。