【发布时间】:2013-04-12 10:44:18
【问题描述】:
我在 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>
我在同一个剃刀视图内有一个按钮。我在下面的按钮 onclick 事件(onclick="schoolname()")中调用了该方法(schoolname()),请参见下文
<div class="logoHeader" style="float: right; width: 15%; padding-top: 5%;">
**<button onclick="schoolname()"> xx</button>**
</div>
但是。它不起作用。我的方法没有被调用。我不知道这个任务。你有什么想法吗?以及如何解决这个问题?
Updated:
我知道程序是 throw 控制器,action result,get,post,java script,jquery 等,所有。我的问题是 @functions 有什么用?在剃刀看来。我知道它有助于在 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>
我使用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>
谢谢盖伊的。
【问题讨论】:
-
MVC 不是 WebForms。它们完全不同,没有太多共同点。这是连接服务器/客户端方法的一种完全不同的方式。我强烈建议您阅读 asp.net 上的
getting started指南 asp.net/mvc/overview/getting-started -
拇指规则:始终查看生成的 html 并问自己:如果我是浏览器,我被要求调用的函数在哪里?看不出来?然后就不行了。
-
所以我不能在我的按钮点击事件中调用那个函数。对吗?
-
没有。它必须以某种方式连接到服务器。请观看视频或阅读教程。 MVC 是如此不同的野兽,很容易混淆。
-
我想我只是笑了笑,然后看了哭泣游戏的“淋浴场景”。
标签: c# asp.net-mvc razor asp.net-mvc-4