我如何定义一个方法在剃刀?
当前回答
MyModelVm.cs
public class MyModelVm
{
public HttpStatusCode StatusCode { get; set; }
}
Index.cshtml
@model MyNamespace.MyModelVm
@functions
{
string GetErrorMessage()
{
var isNotFound = Model.StatusCode == HttpStatusCode.NotFound;
string errorMessage;
if (isNotFound)
{
errorMessage = Resources.NotFoundMessage;
}
else
{
errorMessage = Resources.GeneralErrorMessage
}
return errorMessage;
}
}
<div>
@GetErrorMessage()
</div>
您也可以使用下面的代码块。它更干净,功能更丰富。你也可以在上面插入变量,在下面插入函数。而不是使用2个单独的代码块。
@{
string exampleVariable = "just an example variable";
string anotherExampleVariable = "just another example variable";
string GetErrorMessage()
{
var isNotFound = Model.StatusCode == HttpStatusCode.NotFound;
string errorMessage;
if (isNotFound)
{
errorMessage = Resources.NotFoundMessage;
}
else
{
errorMessage = Resources.GeneralErrorMessage
}
return errorMessage;
}
}
其他回答
下面是如何在ASP中编写列表助手。NET Core 3
现在,您可以在代码块中声明的方法体中包含HTML标记,就像以前一样作为本地方法,或者在@functions块中包含HTML标记。方法应该返回 void,或者Task(如果需要异步处理):
@{
void Template(string[] listItems, string style)
{
<ul>
@foreach (var listItem in listItems)
{
<li class="@style">@listItem</li>
}
</ul>
}
}
MyModelVm.cs
public class MyModelVm
{
public HttpStatusCode StatusCode { get; set; }
}
Index.cshtml
@model MyNamespace.MyModelVm
@functions
{
string GetErrorMessage()
{
var isNotFound = Model.StatusCode == HttpStatusCode.NotFound;
string errorMessage;
if (isNotFound)
{
errorMessage = Resources.NotFoundMessage;
}
else
{
errorMessage = Resources.GeneralErrorMessage
}
return errorMessage;
}
}
<div>
@GetErrorMessage()
</div>
您也可以使用下面的代码块。它更干净,功能更丰富。你也可以在上面插入变量,在下面插入函数。而不是使用2个单独的代码块。
@{
string exampleVariable = "just an example variable";
string anotherExampleVariable = "just another example variable";
string GetErrorMessage()
{
var isNotFound = Model.StatusCode == HttpStatusCode.NotFound;
string errorMessage;
if (isNotFound)
{
errorMessage = Resources.NotFoundMessage;
}
else
{
errorMessage = Resources.GeneralErrorMessage
}
return errorMessage;
}
}
你也可以使用@{}块来创建函数:
@{
async Task<string> MyAsyncString(string input)
{
return Task.FromResult(input);
}
}
然后在你的剃须刀页后面:
<div>@(await MyAsyncString("weee").ConfigureAwait(false))</div>
在razor中定义一个函数非常简单。
@functions {
public static HtmlString OrderedList(IEnumerable<string> items)
{ }
}
你可以在任何地方调用函数。就像
@Functions.OrderedList(new[] { "Blue", "Red", "Green" })
但是,同样的工作也可以通过helper完成。举个例子
@helper OrderedList(IEnumerable<string> items){
<ol>
@foreach(var item in items){
<li>@item</li>
}
</ol>
}
So what is the difference?? According to this previous post both @helpers and @functions do share one thing in common - they make code reuse a possibility within Web Pages. They also share another thing in common - they look the same at first glance, which is what might cause a bit of confusion about their roles. However, they are not the same. In essence, a helper is a reusable snippet of Razor sytnax exposed as a method, and is intended for rendering HTML to the browser, whereas a function is static utility method that can be called from anywhere within your Web Pages application. The return type for a helper is always HelperResult, whereas the return type for a function is whatever you want it to be.
你也可以用Func这样做
@{
var getStyle = new Func<int, int, string>((width, margin) => string.Format("width: {0}px; margin: {1}px;", width, margin));
}
<div style="@getStyle(50, 2)"></div>
推荐文章
- 剃刀视图抛出“名称‘模型’在当前上下文中不存在”
- Linq-to-Entities Join vs GroupJoin
- 为什么字符串类型的默认值是null而不是空字符串?
- 在list中获取不同值的列表
- 组合框:向项目添加文本和值(无绑定源)
- HTML按钮调用MVC控制器和动作方法
- 如何为ASP.net/C#应用程序配置文件值中的值添加&号
- 从System.Drawing.Bitmap中加载WPF BitmapImage
- 如何找出一个文件存在于c# / .NET?
- 为什么更快地检查字典是否包含键,而不是捕捉异常,以防它不?
- [DataContract]的命名空间
- string. isnullorempty (string) vs. string. isnullowhitespace (string)
- 完全外部连接
- ASP。NET MVC 3 Razor:在head标签中包含JavaScript文件
- 在foreach循环中编辑字典值