英文:
ASP.NET MVC: can a controller call another controller and tell it to delete data?
问题
我有一个名为Datasets的模型类,如下所示:
public class Datasets
{
public Datasets()
{
if (id == null)
id = Guid.NewGuid().ToString();
}
[Key]
public string id { get; set; }
public string name { get; set; }
}
对于这个模型类,我有一个Delete.cshtml
页面,允许用户从页面和数据库中删除数据集:
<form asp-action="Delete">
<input type="hidden" asp-for="id" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
DatasetsController.cs
// GET: Datasets/Delete/5
public async Task<IActionResult> Delete(string id)
{
if (id == null)
{
return NotFound();
}
var datasets = await _context.Datasets
.FirstOrDefaultAsync(m => m.id == id);
if (datasets == null)
{
return NotFound();
}
return View(datasets);
}
// POST: Datasets/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id)
{
var datasets = await _context.Datasets.FindAsync(id);
_context.Datasets.Remove(datasets);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
在数据库中还可以找到其他表中的唯一id。我希望删除表中匹配相同id的所有行。
其他控制器具有一个名为:
public string dataset { get; set; }
如何通知DatasetsController中的其他控制器,它们需要删除匹配id的相应数据(如果id == dataset,则删除)。
我可以从另一个控制器中调用特定的控制器吗?我有5个其他控制器需要通知删除数据调用。
我尝试了以下但没有作用:
RedirectToAction("ATSController", "Delete", new { Delete = id });
谢谢!
英文:
I have Datasets model class like this:
public class Datasets
{
public Datasets()
{
if (id == null)
id = Guid.NewGuid().ToString();
}
[Key]
public string id { get; set; }
public string name { get; set; }
}
For that model class, I have a Delete.cshtml
page which allows the user to delete the dataset from the page and thus the database:
<form asp-action="Delete">
<input type="hidden" asp-for="id" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
DatasetsController.cs
// GET: Datasets/Delete/5
public async Task<IActionResult> Delete(string id)
{
if (id == null)
{
return NotFound();
}
var datasets = await _context.Datasets
.FirstOrDefaultAsync(m => m.id == id);
if (datasets == null)
{
return NotFound();
}
return View(datasets);
}
// POST: Datasets/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id)
{
var datasets = await _context.Datasets.FindAsync(id);
_context.Datasets.Remove(datasets);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
The unique id
is also found in other tables in the database. I wish to delete all the rows in the table that match the same id
.
Other controllers have a key called:
public string dataset { get; set; }
How do I notify the other controllers from the DatasetsController
that they need to delete the corresponding data that matches the id
(if id == dataset, then delete).
Can I call a specific controller from another controller? I have 5 other controllers that would need to be notified of the delete data call.
I have a tried the following but it did nothing
RedirectToAction("ATSController", "Delete", new { Delete = id });
Thank you!
答案1
得分: 1
在MVC应用程序中,UI部分和业务逻辑之间应该有一个分离,由控制器表示。一种构建MVC应用程序的方法是保持控制器非常薄,换句话说,控制器执行模型验证,然后调用一个服务方法,该方法执行所需的操作,然后返回结果。
如果以这种方式组织事务,那么您将不必从一个控制器调用另一个控制器,而是使用服务进行工作,并可以通过创建多个针对业务逻辑特定领域的服务来继续分离业务逻辑。
这样可以使您能够单元测试逻辑,而无需担心控制器。
然后,您可以基于接口构建服务,因此每个服务都将有一个描述其功能的接口,这就是其契约。
最后一部分是通过依赖注入将您需要的任何服务注入到需要该服务的任何控制器中,这在MVC框架中得到了很好的支持。
这将为您构建应用程序提供坚实的基础。
英文:
In an MVC application there is or there should be a separation between UI stuff, represented by controllers and the business logic which is where stuff happens.
One way to build an MVC application is to keep the controllers very thin, in other words controllers do the model validation then call let's say a service method which does what needs done and then it returns the result.
If you organise things this way then you will not have to call a controller from another controller, you work with services and you can continue to separate your business logic by creating multiple services each targeting a specific area of your business logic.
That makes it then possible to unit test the logic without worrying about controllers at all.
You then build services based on interfaces, so each service would have an interface which describes what it does, this is its contract.
The final piece is that you inject whatever services you need in whatever controller needs that service, via dependency injection which is very well supported by the MVC framework.
This would give you a solid foundation to build your app on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论