如何在视图组件中发送发布请求
我目前正在研究ASP.NET Core 6.0 MVC应用程序(带有实体框架),并且我正在尝试在不刷新页面的情况下执行CRUD操作。就我的研究带走了我,最好使用视图组件。我使用以下代码:
@await Component.InvokeAsync("AccountPhonesListEdit", new{accountNumber = Model.AcctNbr, typeOfCall = "Index", phoneId = -1})
InvokeAsync
可以作为调用视图组件控制器调用各种CRUD功能的方式,我有:我有:我有:
public async Task<IViewComponentResult> InvokeAsync(int accountNumber, string typeOfCall, int phoneId)
{
switch (typeOfCall)
{
case "Create":
await AccountPhonesCreate(accountNumber);
break;
case "Delete":
await AccountPhonesDelete(phoneId);
break;
}
return await AccountPhonesListEdit(accountNumber);
}
我有:我有我的视图组件中的一个按钮,单击时应调用删除。它是这样的工作:
<button class="form-control bg-danger text-white"
onclick="@(async () => await Component.InvokeAsync("InvokeAsync", new{accountNumber = ap.AcctNbr, typeOfCall = "Delete", phoneId = ap.PhoneSequence}))">Delete</button>
当我单击此按钮时,整个页面刷新,并且实际上没有执行删除功能。我不认为我的删除函数有任何问题,但是仅对于上下文而言,这就是视图组件中的删除函数:
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<bool> AccountPhonesDelete(int phoneSequence)
{
var accountPhone = await _context.AccountPhones.FindAsync(phoneSequence);
if (accountPhone != null)
{
_context.AccountPhones.Remove(accountPhone);
await _context.SaveChangesAsync();
return true;
}
return false;
}
现在,我进入了调试模式以检查所有内容时都会检查参数的值。当页面首次加载时,调用视图组件的原始InvokeAsync
时,InvokeAsync
的参数是我们期望的 - acctnbr,“ index”和-1 ,分别用于帐户名称,typeofcall和PhoneId。但是,当我使用按钮单击调用删除函数时,将传递相同的参数 - acctnbr,“ index”和-1-将传递!为什么这是?如何在不重新加载页面的情况下正确调用Crud操作来正确调用CRUD操作?事先感谢您的帮助!
I am currently working on an ASP.NET Core 6.0 MVC application (with Entity Framework), and I am trying to be able to perform CRUD operations without refreshing the page. As far as my research has taken me, it seems best to use View Components for this. I have the initial view component working (it renders the GET functionality), using the following code:
@await Component.InvokeAsync("AccountPhonesListEdit", new{accountNumber = Model.AcctNbr, typeOfCall = "Index", phoneId = -1})
where InvokeAsync
works as a way to call the various CRUD functionalities for the view component controller, as so:
public async Task<IViewComponentResult> InvokeAsync(int accountNumber, string typeOfCall, int phoneId)
{
switch (typeOfCall)
{
case "Create":
await AccountPhonesCreate(accountNumber);
break;
case "Delete":
await AccountPhonesDelete(phoneId);
break;
}
return await AccountPhonesListEdit(accountNumber);
}
I have a button in my view component that, when clicked, should be calling delete. It works like this:
<button class="form-control bg-danger text-white"
onclick="@(async () => await Component.InvokeAsync("InvokeAsync", new{accountNumber = ap.AcctNbr, typeOfCall = "Delete", phoneId = ap.PhoneSequence}))">Delete</button>
When I click this button, the entire page refreshes, and the delete function is not actually performed. I don't believe there is anything wrong with my delete function, but just for context here is the delete function in the view component:
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<bool> AccountPhonesDelete(int phoneSequence)
{
var accountPhone = await _context.AccountPhones.FindAsync(phoneSequence);
if (accountPhone != null)
{
_context.AccountPhones.Remove(accountPhone);
await _context.SaveChangesAsync();
return true;
}
return false;
}
Now, I've gone into debug mode to check the values of parameters when everything is called. When the original InvokeAsync
to call the view component is called when the page first loads, the parameters for InvokeAsync
are what we would expect -- AcctNbr, "Index" and -1, for accountNumber, typeOfCall and phoneId respectively. However, when I call the delete function with the button click, the same parameters -- AcctNbr, "Index" and -1 -- are passed in! Why is this? How can I get my View Component to properly call CRUD operations without reloading the page? Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论