创建表单并绑定到没有数据库的类

我是学习 MVC 的新手,确实需要一些帮助,因为我正在尝试从 ASP.NET Web 表单继续学习。我有一个用于所有数据库事务的自定义 Web API,该 API 非常高效且可靠。我已经在 .NET Core 中重新编码了它。


我的问题是,我发现大多数与 MVC 相关的模型绑定示例都是由实体框架示例组成的。我正在寻求帮助,展示如何使用 get() post(form) 操作将 ViewModel 链接到控制器。我需要了解如何绑定到单选按钮列表等......


我正在使用下面的类,它删除了数据库连接以简化答案/建议。


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; }

}

我正在尝试创建一个视图,它将显示这些数据,然后允许我回发用户编辑。我故意让示例保持简单,因为一旦我弄清楚如何回发,我就可以将更新后的数据传递给 Web API 来完成我需要完成的工作。只是想弄清楚我如何绑定到这种类型的类,这让我感到沮丧。


翻阅古今
浏览 74回答 2
2回答

阿波罗的战车

对于单选按钮,我将向您的 BuildSearch 类添加一个名为 GeoAreaId 的属性。单选按钮选择将在回发时模型绑定到此属性。因此,您的 BuildSearch 类变为public class BuildSearch{&nbsp; &nbsp; //Bootstrap date entry&nbsp; &nbsp; public DateTime StartDate { get; set; }&nbsp; &nbsp; //Bootstrap date entry&nbsp; &nbsp; public DateTime EndDate { get; set; }&nbsp; &nbsp; public int GeoAreaId { get; set; } //added field&nbsp; &nbsp; //Need this bound to a radio button list&nbsp; &nbsp; public List<GeoArea> GeoAreas { get; set; }&nbsp; &nbsp; public BuildSearch()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; GeoAreas = new List<GeoArea>();&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;StartDate = DateTime.Parse(DateTime.Now.AddDays(-31).ToShortDateString());&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; EndDate = DateTime.Parse(DateTime.Now.ToShortDateString());&nbsp; &nbsp; &nbsp; &nbsp; GeoAreas.Add(new GeoArea { GeoAreaItem = "Region", Id = 0 });&nbsp; &nbsp; &nbsp; &nbsp; GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager1", Id = 1 });&nbsp; &nbsp; &nbsp; &nbsp; GeoAreas.Add(new GeoArea { GeoAreaItem = "Manager2", Id = 2 });&nbsp; &nbsp; }&nbsp; &nbsp; public class GeoArea&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string GeoAreaItem { get; set; }&nbsp; &nbsp; }}你的控制器中的 get 方法看起来像这样&nbsp; &nbsp; public IActionResult Search()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var buildSearch = new BuildSearch();&nbsp; &nbsp; &nbsp; &nbsp; return View(buildSearch);&nbsp; &nbsp; }你的视图需要看起来像这样@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers@model&nbsp; BuildSearch<form asp-controller="Home" asp-action="Search" method="post">&nbsp; &nbsp;&nbsp; &nbsp; <label asp-for="StartDate">Start date</label>&nbsp; &nbsp; <input asp-for="StartDate" />&nbsp; &nbsp; <label asp-for="EndDate">End date</label>&nbsp; &nbsp; <input asp-for="EndDate" />&nbsp; &nbsp; <fieldset>&nbsp; &nbsp; &nbsp; &nbsp; <legend>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GeoArea&nbsp; &nbsp; &nbsp; &nbsp; </legend>&nbsp; &nbsp; &nbsp; &nbsp; @foreach (var item in Model.GeoAreas)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="GeoAreaId" value="@item.Id" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @item.GeoAreaItem&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </fieldset>&nbsp; &nbsp; <input type="submit" /></form>对于单选按钮,请注意 name 属性如何与我添加到 BuildSearch 类中的新属性 GeoAreaId 相匹配。这对于模型绑定的工作非常重要。然后你的控制器中的 post 方法将需要如下所示&nbsp; &nbsp; [HttpPost]&nbsp; &nbsp; [ValidateAntiForgeryToken]&nbsp; &nbsp; public IActionResult Search(BuildSearch buildSearch)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; }要查看发生了什么,请在此方法中设置一个断点。运行代码,在表单中输入一些值,然后单击提交。当代码停止在 buildSearch 上时,您将看到模型绑定已起作用。属性 StartDate、EndDate 和 GeoAreaId 将包含表单中所需的值。

元芳怎么了

将数据放入视图模型中,您将执行类似的操作public async Task<IActionResult> Index(){        var movies = await _context.Movies.ToList();    if (movies == null)    {        return NotFound();    }    return View(movies);}然后,您将需要该表格来对您的操作进行后续操作,例如[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 类中,如下所示@model MvcMovie.Models.Movie@{    ViewData["Title"] = "Edit";}<h1>Edit</h1><h4>Movie</h4> etc etc
打开App,查看更多内容
随时随地看视频慕课网APP