2

I have one method in Inside of Razor view.

<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

<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 .

Finished my self :

See below my Razor View Function :

<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 .

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

Thanks Guy's .

4

1 回答 1

1

我的朋友,你这样做完全错了,因为 Asp.net Mvc 不像经典的 Asp.net Web 表单。我建议你看看这本书以获取更多信息。但是,要解决您的问题,您可以在 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>
于 2013-04-19T05:51:01.927 回答