我刚刚使用ASP.NET Core MVC为CRUD创建了一个简单的项目。编辑是使用局部视图。编辑本身是有效的,但只要局部视图显示,主视图上的按钮就不起作用,甚至不能进入相应的功能。我在哪里做错了什么?我该如何解决这个问题?附件是我认为相关的所有代码片段。
Cost.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace CarpetWash.Models
{
public class Cost
{
[Key]
public int Id { get; set; }
public DateTime? Date { get; set; }
public decimal? Expense { get; set; }
public string? Item { get; set; }
public string? Description { get; set; }
public string? Location { get; set; }
public string? Note { get; set; }
}
}
CarpetWashContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace CarpetWash.Models
{
public class CarpetWashContext : DbContext
{
public DbSet<Cost> Costs { get; set; }
public CarpetWashContext(DbContextOptions<CarpetWashContext> options) : base(options)
{
}
public CarpetWashContext()
{
}
}
}
CarpetWashViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CarpetWash.Models
{
public class CarpetWashViewModel
{
public List<Cost> Costs { get; set; }
public Cost SelectedCost { get; set; }
public string DisplayMode { get; set; }
}
}
Cost.cshtml
@model CarpetWash.Models.CarpetWashViewModel
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css" />
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function () {
$("#tblCosts").DataTable({
"aLengthMenu": [[5, 10, 25, -1], [5, 10, 25, "All"]],
"iDisplayLength": 10,
});
});
</script>
</head>
<h1>Costs</h1>
<body>
<form method="post">
@{
if (Model.SelectedCost != null)
{
if (Model.DisplayMode == "ReadWrite")
{
Html.RenderPartial("EditCost", Model.SelectedCost);
}
}
if (Model.DisplayMode == "WriteOnly")
{
Html.RenderPartial("InsertCost", new Earn());
}
}
<div>
<input type="submit" value="Add New Cost" formaction= "/CarpetWash/newCost" class="btn btn-primary" tabindex="1" />
</div>
<div>
<table id="tblCosts" class="table">
<thead>
<tr>
<th>Id</th>
<th>Date</th>
<th>Expense</th>
<th>Item</th>
<th>Description</th>
<th>Location</th>
<th>Note</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@if (Model.Costs != null)
{
foreach (var item in Model.Costs)
{
if (Model.SelectedEarn != null)
{
if (item.Id == Model.SelectedCost.Id)
{
@:<tr class=" SelectedItem">
}
else
{
@:<tr>
}
}
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Expense)
</td>
<td>
@Html.DisplayFor(modelItem => item.Item)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.Note)
</td>
<td><input type="submit" value="Edit" class="btn btn-primary Edit" formaction="/CarpetWash/selectCost/@item.Id " /></td>
<td>
<input type="submit" value="Delete" class="btn btn-primary Delete" formaction="/CarpetWash/deleteCost/@item.Id" />
</td>
@:</tr>
}
}
</tbody>
</table>
</div>
</form>
</body>
部分视图EditCost.cshtml
@model CarpetWash.Models.Cost
<h4>Cost</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="SelectCost">
<div class="form-group">
<label asp-for="Id" class="control-label"></label>
@Html.TextBoxFor(m => m.Id, new { @readonly = "readonly" })
<span asp-validation-for="Id" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Date" class="control-label"></label>
@Html.TextBoxFor(m => m.Date, new { @title = "Please input a date (i.e. 2020-07-01)" })
<span asp-validation-for="Date" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Expense" class="control-label"></label>
@Html.TextBoxFor(m => m.Expense, new { @title = "Please input a string" })
<span asp-validation-for="Expense" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Item" class="control-label"></label>
@Html.TextBoxFor(m => m.Item, new { @title = "Please input a string" })
<span asp-validation-for="Item" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
@Html.TextBoxFor(m => m.Description, new { @title = "Please input a string" })
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Location" class="control-label"></label>
@Html.TextBoxFor(m => m.Location, new { @title = "Please input a string" })
<span asp-validation-for="Location" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Note" class="control-label"></label>
@Html.TextBoxFor(m => m.Note, new { @title = "Please input a string" })
<span asp-validation-for="Note" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn" formaction="/CarpetWash/updateCost/@Model.Id" />
<input type="submit" value="Cancel" class="btn" formaction="/CarpetWash/cost" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
CarpetWashController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using JiaCarpetWash.Common;
using JiaCarpetWash.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace CarpetWash.Controllers
{
public class CarpetWashController : Controller
{
private readonly CarpetWashContext _context;
private readonly IOptions<AppSettings> _mySettings;
private readonly string _dbConnectString;
private readonly IMemoryCache _MemoryCache;
public CarpetWashController(CarpetWashContext context, IOptions<AppSettings> mySettings, IMemoryCache memCache)
{
_context = context;
_mySettings = mySettings;
_dbConnectString = _mySettings.Value.DBConnectionString;
_MemoryCache = memCache;
}
// GET: CarpetWashController
public async Task<IActionResult> Cost()
{
CarpetWashViewModel model = new CarpetWashViewModel();
model.Costs = await _context.Costs.ToListAsync();
model.SelectedCost = null;
return View(model);
}
public async Task<IActionResult> SelectCost(int? id)
{
CarpetWashViewModel model = new CarpetWashViewModel();
model.Costs = await _context.Costs.ToListAsync();
var cost = await _context.Costs.FindAsync(id);
if (cost == null)
{
return NotFound();
}
model.SelectedCost = cost;
model.DisplayMode = "ReadWrite";
return View("Cost", model);
}
[HttpPost]
public async Task<IActionResult> SelectCost(int id)
{
CarpetWashViewModel model = new CarpetWashViewModel();
model.Costs = await _context.Costs.ToListAsync();
var cost = await _context.Costs.FindAsync(id);
if (cost == null)
{
return NotFound();
}
model.SelectedCost = cost;
model.DisplayMode = "ReadWrite";
return View("Cost", model);
}
// GET: CarpetWashController/UpdateCost/5
public async Task<IActionResult> UpdateCost(int? id)
{
CarpetWashViewModel model = new CarpetWashViewModel();
if (id == null)
{
return NotFound();
}
var cost = await _context.Costs.FindAsync(id);
if (cost == null)
{
return NotFound();
}
model.SelectedCost = cost;
model.DisplayMode = "ReadWrite";
return View("Cost", model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateCost(int id, [Bind("Id,Date,Expense,Item,Description,Location,Note")] Cost cost)
{
if (id != cost.Id)
{
return NotFound();
}
CarpetWashViewModel model = new CarpetWashViewModel();
model.SelectedCost = cost;
if (ModelState.IsValid)
{
model.DisplayMode = "ReadOnly";
try
{
_context.Update(cost);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!_context.Costs.Any(e => e.Id == id))
{
return NotFound();
}
else
{
throw;
}
}
model.SelectedCost = null;
}
else
{
model.DisplayMode = "ReadWrite";
}
model.Costs = await _context.Costs.ToListAsync();
return View("Cost", model);
}
// GET: CarpetWashController/DeleteCost/5
public async Task<IActionResult> DeleteCost(int? id)
{
if (id == null)
{
return NotFound();
}
var cost = await _context.Costs
.FirstOrDefaultAsync(m => m.Id == id);
if (cost == null)
{
return NotFound();
}
return View("Cost");
}
// POST: CarpetWashController/DeleteCost/5
[HttpPost, ActionName("DeleteCost")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteCostConfirmed(int id)
{
CarpetWashViewModel model = new CarpetWashViewModel();
var cost = await _context.Costs.FindAsync(id);
if (cost != null)
{
DeleteCostById(id);
_context.Costs.Remove(cost);
await _context.SaveChangesAsync();
}
model.Costs = await _context.Costs.ToListAsync();
model.SelectedCost = null;
model.DisplayMode = "ReadOnly";
return View("Cost", model);
}
private void DeleteCostById(int id)
{
using SqlConnection connection = new SqlConnection(_dbConnectString);
using SqlCommand command = new SqlCommand("RemoveCost", connection);
command.CommandType = CommandType.StoredProcedure;
using var da = new SqlDataAdapter(command);
}
}
}
发布于 2020-07-16 07:55:50
因为您的分部视图包含一个表单,并且在Cost.cshtml
中它也有一个将分部视图放入表单中的form.You,但是您不能嵌套forms.So,所以可以将部分视图移到表单之外。下面是一个演示: CarpetWashController.cs:
public IActionResult Index()
{
return View();
}
public IActionResult Cost()
{
CarpetWashViewModel c = new CarpetWashViewModel();
List<Cost> cs = new List<Cost> { new Cost { Id = 1, Item = "item1", Date = new DateTime(), Expense = 1, Description="d1", Note="n1", Location="l1"} };
Cost c1 = new Cost { Id = 2, Item = "item2", Date = new DateTime(), Expense = 2, Description = "d2", Note = "n2", Location = "l2" };
cs.Add(c1);
c.SelectedCost = c1;
c.Costs = cs;
c.DisplayMode = "ReadWrite";
return View(c);
}
public IActionResult newCost() {
return View("Index");
}
public IActionResult selectCost(int id)
{
return View("Index");
}
public IActionResult deleteCost(int id)
{
return View("Index");
}
Cost.cshtml:
<div>
<div>
@if (Model.SelectedCost != null)
{
if (Model.DisplayMode == "ReadWrite")
{
Html.RenderPartial("EditCost", Model.SelectedCost);
}
}
</div>
<form method="post">
<input type="submit" value="Add New Cost" formaction="/CarpetWash/newCost" class="btn btn-primary" tabindex="1" />
<div>
<table id="tblCosts" class="table">
<thead>
<tr>
<th>Id</th>
<th>Date</th>
<th>Expense</th>
<th>Item</th>
<th>Description</th>
<th>Location</th>
<th>Note</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="submit" value="Edit" class="btn btn-primary Edit" formaction="/CarpetWash/selectCost/1 " /></td>
<td>
<input type="submit" value="Delete" class="btn btn-primary Delete" formaction="/CarpetWash/deleteCost/1" />
</td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
结果:
https://stackoverflow.com/questions/62927585
复制相似问题