我正在阅读一本关于MVC,Bootstrap和Knockoutjs的书,我很难产生我想要的结果。这是我第一次使用knockoutjs,而且我对编码仍然相对较新 - 总的来说。这是我在本教程中尝试完成的任务。
我已经下载了Knockoutjs库,并在@RenderSection("Scripts", false)
之上的_layout.cshtml文件中引用它,就像这样..
<environment names="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</environment>
<environment names="Staging,Production">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/bootstrap.min.js"
asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal">
</script>
<script src="~/js/site.min.js" asp-append-version="true"></script>
<script src="~/lib/knockoutjs/dist/knockout.js"></script>
</environment>
@RenderSection("Scripts", false)
</body>
然后我使用以下代码
创建了一个名为Basic.cshtml的视图 @{
ViewBag.Title = "Basic";
}
<h2>Hello <span data-bind="text: name"></span></h2>
@section Scripts {
<script>
function ViewModel() {
this.name = "Not Eric McQuiggan"
};
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
</script>
}
值得注意的是,当我输入时,Intellisense没有采用@section Scripts语法。
理论上,当我使用_layout.cshtml导航到XXXX / home / basic,然后将来自knockoutjs的数据绑定值传递给show&#39; Hello Not Eric McQuiggan&#39; 。但是,在调试时,页面只显示标题文本(&#39; Hello&#39;)而不显示任何其他内容。
显然,这意味着脚本没有运行。我试图找到关于我可能错过的任何事情的其他信息,但我处于死胡同。我相信它最终会变得很小,但任何帮助都会受到赞赏。
提前致谢!
答案 0 :(得分:2)
ASP.NET Core引入了对使用多个environments。
的支持如果您未指定,则默认环境为#include <stdio.h>
#include <stdlib.h>
double calculateBMI(double weight, double height);
int main(void)
{
printf("Calculate your BMI\n"); //Calculation of body mass index (BMI)
double w, h, ret;
double BMI = w / (h*h);
ret = calculateBMI(BMI);
printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here
scanf("%lf", &w);
printf("Enter your height in metres:\n", h); //Input your height in metres here
scanf("%lf", &h);
printf("Your BMI is %lf\n", ret)
printf("BMI categories:\n");
if (BMI < 18.5)
{
printf("Your BMI is %lf and you are currently underweight.\n");
}
else if (BMI >= 18.5 && BMI <= 24.9)
{
printf("Your BMI is %lf and you are normal weight.\n");
}
else if (BMI >= 25 && BMI <= 29.9);
{
printf("Your BMI is %lf and you are currently overweight.\n");
}
else (BMI >= 30);
{
printf("Your BMI is %lf and you are obese.\n");
}
return 0;
}
double calculateBMI(double weight, double height)
{
double result;
result = weight / (height*height);
return result;
}
。
在您的布局中,您正在使用developmenet
标记帮助程序元素,如果您使用匹配的环境,它只会将其内容写入输出。
因此,在environment
模式下,您的HTML将如下所示:
development
因此,在这种情况下不会加载Kncokout,您应该会在浏览器的控制台中看到<environment names="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</environment>
<!-- this is not included in the outputted HTML when in development mode -->
<!-- <environment names="Staging,Production">
...
</environment> -->
@RenderSection("Scripts", false)
错误。
修复非常简单,您还需要在ko is undefined
脚本部分中包含Knockout参考:
development
注意:您应该确保<environment names="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="~/lib/knockoutjs/dist/knockout.js"></script>
</environment>
位于正确的位置,并且ASP.NET Core配置为从该位置为该文件提供服务。