C# 如何在按钮 onclick 事件 mvc4 中调用方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16097853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:59:29  来源:igfitidea点击:

How to call method in button onclick event mvc4?

c#asp.net-mvcrazorasp.net-mvc-4

提问by

I have one method in Inside of Razor view.

我在 Razor 视图内部有一种方法。

<div style="height: 50px; padding-left: 10%; width: 100%">
                                <b style="font-family: Arial, Helvetica, sans-serif; font-weight: normal; font-size: xx-large">                                        
                                @functions {      
                                    public string schoolname()
                                    {
                                        if (Request.IsAuthenticated && !User.IsInRole("SuperAdmin"))
                                        {
                                            string currentUserName = User.Identity.Name;
                                            return GetHeaderSchoolName(currentUserName);
                                        }
                                        return string.Empty;
                                    }

                                    public string GetHeaderSchoolName(string userName)
                                    {
                                        Guid userId = SchoolBreifcase.Utilities.Common.GetUserId(userName);
                                        SchoolBreifcase.Repositories.AdminRepository adminRepository = new SchoolBreifcase.Repositories.AdminRepository();
                                        SchoolBreifcase.Models.Edmx.UserProfile userProfile = adminRepository.GetUserProfileByUserId(userId);
                                        string CurrentSchoolName = userProfile.School.SchoolName;
                                        return CurrentSchoolName + "xxx";
                                    }
                                                }
                            </div>

I have one button in inside of same razor view.And I called that method(schoolname()) in my below button onclick event (onclick="schoolname()") please see below

我在同一个剃刀视图内有一个按钮。我schoolname()在下面的按钮 onclick 事件 ( onclick="schoolname()") 中调用了该方法 ( ),请参见下文

<div class="logoHeader" style="float: right; width: 15%; padding-top: 5%;">                            
                             **<button onclick="schoolname()"> xx</button>**   
                        </div>

But .Its Not working . My method is does not called .I have no idea for this task . You have Any idea here? and how to do solve this ?

但是。它不工作。我的方法没有被调用。我不知道这个任务。你有什么想法吗?以及如何解决这个问题?

Updated:

I know the procedure are threw controller ,action result,get ,post, java script,jquery,etc ,all . My question is What is use of @functions? in razor view . I know its helping to write server side code in inside of razor view . but i can't call this method in just one button click event . So what is the use it ? Its only used only for when page load .

我知道程序被抛出控制器、动作结果、获取、发布、java 脚本、jquery 等,所有。我的问题是有什么用@functions?在剃刀视图中。我知道它有助于在 razor view 中编写服务器端代码。但我不能只在一个按钮点击事件中调用这个方法。那有什么用呢?它仅用于页面加载时。

Finished my self :

完成我的自我:

See below my Razor View Function :

请参阅下面我的 Razor 视图功能:

<div style="height: 50px; padding-left: 10%; width: 100%">
                                    <b style="font-family: Arial, Helvetica, sans-serif; font-weight: normal; font-size: xx-large">                                        
                                    @functions {      
                                        public string schoolname()
                                        {
                                            if (Request.IsAuthenticated && !User.IsInRole("SuperAdmin"))
                                            {
                                                string currentUserName = User.Identity.Name;
                                                return GetHeaderSchoolName(currentUserName);
                                            }
                                            return string.Empty;
                                        }

                                        public string GetHeaderSchoolName(string userName)
                                        {
                                            Guid userId = SchoolBreifcase.Utilities.Common.GetUserId(userName);
                                            SchoolBreifcase.Repositories.AdminRepository adminRepository = new SchoolBreifcase.Repositories.AdminRepository();
                                            SchoolBreifcase.Models.Edmx.UserProfile userProfile = adminRepository.GetUserProfileByUserId(userId);
                                            string CurrentSchoolName = userProfile.School.SchoolName;
                                            return CurrentSchoolName + "xxx";
                                        }
                                                    }
                                </div>

And I am used javascript:window.location.href="schoolname"for call my function.Its working now .

我用于javascript:window.location.href="schoolname"调用我的函数。它现在可以工作了。

<div class="logoHeader" style="float: right; width: 15%; padding-top: 5%;">                            
                                 **<button onclick='javascript:window.location.href="schoolname"'> xx</button>**   
                            </div>

Thanks Guy's .

谢谢你们 。

回答by ghazyy

You are doing it completely wrong my friend because Asp.net Mvc is not like classic Asp.net Web Forms. I suggest you take look at this bookfor more information. However to solve your problem you can easily do this in CShtml :

我的朋友,你完全错了,因为 Asp.net Mvc 不像经典的 Asp.net Web Forms。我建议你看看这本书以获取更多信息。但是要解决您的问题,您可以在 CShtml 中轻松执行此操作:

@{
var result = Request["YourButtonSpecialName"];   
 }
@if (result=="xx")
{
   <div>
button clicked
</div>    
}
<div class="logoHeader" style="float: right; width: 15%; padding-top:5%;">
<form action="YOUR CURRENT PATH">
<button type="submit" name="YourButtonSpecialName" value="xx"/>
</form>
</div>