html按钮如何调用c#文件,在cshtml中调用的C#函数

本文讨论了尝试在CSHTML中直接调用C#函数的问题。专家建议不要将函数放在视图中,而是使用JavaScript和C#的组合。推荐的方法是通过JavaScript的点击事件触发AJAX请求,然后在控制器中处理删除操作。这样可以保持MVC模式并避免混淆服务器端和客户端代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am not able to call a function in my CSHTML code, despite many efforts. Is a construction like this even roughly possible to work?

***Function***

@functions {

public void deleteRecord(int id, Database db)

{

db.Execute("DROP INDEX "+id+"ON Students");

}

}

***Button calling that function***

Delete

db is declared earlier SQL database

Talk1:

No, the code you put in an onclick handler has to be JavaScript. You can't magically allow a button in your page to call a function on your server by putting it in your razor file.

Talk2:

No. You're confusing serverside/clientside and you're destroying the MVC pattern here. But firing of a service call is easy.

Solutions1

This is really, really bad approach. Firstly, you are breaking MVC rules which are strict - model for data, view for presenting data and controller for ensuring interaction between model and view layer.

Secondly, you are trying to query database via raw SQL queries. It is not as bad as first problem, but did you consider using ORM like Entity Framework or NHibernate.

In your case, I propose you to use piece of JavaScript and C# to reach your target.

It should looks more or less like this:

File view.cshtml

Delete

File script.js

$("#deleteButton").click(function() {

$.get("/SomePage/Delete/12")

.done(function(obj) {

// display result or whatever you want

});

})

File SomePageController.cs

public ActionResult Delete(int id)

{

// delete object from database

}

Solutions2Is a construction like this even roughly possible to work?

If it is, it really shouldn't be. This is mixing concerns in a very bad (difficult to debug/support) way.

Your server-side code should be in your server-side objects, not in your client-side views. If this is MVC, that method probably belongs on a model. If this is WebForms, that method probably still belongs on a model or possibly in code-behind.

Don't put methods in your view. You probably can't anyway, since a view isn't a class and methods need to be in classes. But even if you find some hack to allow you to do this, don't.

Edit: I also just noticed that you're trying to invoke this method from a client-side action. Even if you get this to compile, this won't work. Client-side code can't directly call methods in server-side code, and vice-versa. Think of a web application as actually being two applications, one running on the server and one running in the browser. They can communicate with each other, but they're different languages running on different platforms on entirely different computers, they can't see each other's code.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值