c# - 从 Razor 视图中的表单返回数组或模型列表以在提交时执行操作
问题描述
我正在做一个学习项目,在该项目中我创建了一个视图,该视图显示了通过调用 api 填充的已删除图书列表(isActive = false)。该视图在每本书前面都有一个书籍列表+一个复选框,因此在单击我们要设置为活动的书籍并点击保存按钮时,它会将被选中的书籍返回给控制器中的操作。这是我正在使用的视图:
@model IEnumerable<MvcBooksList.Models.Book>
@{
ViewData["Title"] = "Delisted Books";
}
@if (Model != null)
{
@using (Html.BeginForm("DelistedForm", "Book", FormMethod.Post, new { @id = "myForm" }))
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.BookName)
</th>
<th>
@Html.DisplayNameFor(model => model.Author)
</th>
<th>
@Html.DisplayNameFor(model => model.Publisher)
</th>
<th>
Enlist
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BookName)
@Html.HiddenFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
@Html.HiddenFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
@Html.HiddenFor(modelItem => item.Publisher)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.IsActive)
</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Save" /> <input type = "button" value = "Cancel" onclick = "location.href='@Url.Action("Index", "Home")'" />
}
}
else
{
<h3>No Books currently delisted</h3>
}
这是模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MvcBooksList.Models
{
public class Book
{
public string BookName { get; set; }
public string Author { get; set; }
public string Category { get; set; }
public string Subcategory { get; set; }
public string Publisher { get; set; }
public bool IsActive { get; set; }
public double? Price { get; set; }
public int Pages { get; set; }
public string AddedBy { get; set; }
public string ModifiedBy { get; set; }
public DateTime AddedOn { get; set; }
public DateTime LastModifiedOn { get; set; }
}
}
这是控制器:
[HttpPost]
public async Task<ActionResult> DelistedForm(IEnumerable<Book> fromDelist)
{
foreach (var item in fromDelist)
{
if (item.IsActive == true)
//CALL
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = baseAddressOfBookApi;
var resopnse = await client.GetAsync("Book/EnlistBook?id="+item.BookName.ToString());
if (!resopnse.IsSuccessStatusCode)
{
return View(null);
}
}
}
}
return RedirectToAction("Index","Home");
}
不要介意动作中的代码。
所以当我按下提交按钮时,没有任何东西传递给操作。fromDelist 集合显示计数为 0。那么,我做错了什么。是否有可能我可以返回检查过的书籍的名称。
图书控制器 ::
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MvcBooksList.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace MvcBooksList.Controllers
{
public class BookController : Controller
{
readonly Uri baseAddressOfBookApi;
public BookController(IConfiguration configuration)
{
baseAddressOfBookApi = new Uri(configuration.GetSection("ApiAddress:BookAPI").Value);
}
/*
public ActionResult Edit(string name)
{
return View(name); //repalce with the name of view
}
public ActionResult Delete(string name)
{
return View(name); //repalce with the name of view
}
public ActionResult Delist(string name)
{
return View(name); //repalce with the name of view
}
*/
public async Task<ActionResult> ViewDelistedBooks()
{
List<Book> bookdetails = new List<Book>();
using (HttpClient client = new HttpClient())
{
client.BaseAddress = baseAddressOfBookApi;
var response = await client.GetAsync("Book/ViewDelistedBooks");
if (response.IsSuccessStatusCode)
{
var BookResponse = response.Content.ReadAsStringAsync().Result;
bookdetails = JsonConvert.DeserializeObject<List<Book>>(BookResponse);
}
}
return View(bookdetails);
}
public ActionResult Cancel()
{
return View("~/Views/Home/Index.cshtml");
}
[HttpPost]
public async Task<ActionResult> DelistedForm(IEnumerable<Book> fromDelist)
{
foreach (var item in fromDelist)
{
if (item.IsActive == true)
//CALL
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = baseAddressOfBookApi;
var resopnse = await client.GetAsync("Book/EnlistBook?id="+item.BookName.ToString());
if (!resopnse.IsSuccessStatusCode)
{
return View(null);
}
}
}
}
return RedirectToAction("Index","Home");
}
}
}
解决方案
尝试用 for 替换 foreach
@model List<MvcBooksList.Models.Book>
@for (int i = 0; i < Model.Count(); i+=1)
{
......
<td>
@Html.DisplayFor(model=> model[i].Publisher)
@Html.HiddenFor(model => model[i].Publisher)
</td>
<td>
@Html.CheckBoxFor(model => model[i].IsActive)
</td>
</tr>
.... and so on for all
并替换表头
<th>
@Html.DisplayNameFor(model => model[0].BookName)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Author)
</th>
推荐阅读
- opendaylight - 使用 Opendaylight 的链路聚合控制协议
- spring - 涡轮机不适用于 spring boot 2.0.6 和 spring cloud Finchley.SR1
- pdf - 创建附加在一起的所有条形图的 pdf
- sql-server - 将实例分配给滚动但重置 10 秒的时间间隔
- android - Android Emulator:设置代理服务器时如何不代理 10.0.2.2?
- java - 不能在内部类中使用变量
- javascript - 如何通过表单输入提交带有子文件夹的文件夹?
- routing - Polymer SPA 的登录页面
- spring - Keycloak Grails Spring - 登录后重定向
- c++ - 在 g++ v8.2.0 中使用 std::experimental::optional