asp.net-core - 避免剃刀页面中代码重复的最简单方法
问题描述
asp dotnet core 2.1 避免代码重复的最简单方法是什么。我有一份问卷,例如问题类型编号,我有这样的代码。
<div class="form-group row">
<label class="control-label col-md-4 offset-md-0 pt-2">@question.Text:</label>
<div class="col-md-2" question-validation key="@question.Id">
@{
var key = question.Id.ToString();
string value = null;
if (Model.Answers.ContainsKey(key) && !string.IsNullOrWhiteSpace(Model.Answers[key]))
{
value = Model.Answers[question.Id.ToString()];
}
}
<div class="form-group" >
<input class="form-control" type="text" question-type="@QuestionType.Number" question-id="@question.Id" onkeypress="return isNumber(event)" value="@value">
</div>
</div>
我想在单个页面上多次使用此代码,但我不想复制代码。
解决方案
The sure way is to make that code a PartialView
once a partial view you can then call it anywhere and pass the model to it
<partial name="myPartialView" />
//Or with a model
@(await Html.PartialAsync("myPartialView", modelToPass))
推荐阅读
- c++ - 为什么我的代码无法处理大型数组输入(> 10000)?
- flutter - 打开和显示目录中的图像
- laravel - 如何使用数据透视表显示不同表格的列?
- microsoft-graph-api - Microsoft Graph - 教育 API - 学生/教师关系
- r - data.table 中的 Computed `by` 子句
- sql - 如何使用 dbGetQuery 命令正确编写查询?
- nightwatch - 如何通过 Nightwatch 中的导航项测试重定向
- vue.js - 需要直接改变 Vue.js 中的属性,即使不建议这样做
- scala - scala 从配置文件中读取对象数组
- java - 使用子选择和窗口函数执行复杂查询时出现 SQLite Java 错误