javascript链接到html,从javascript链接到.cshtml视图

如评论中所述,您无法直接提供.cshtml文件,但是,如果您愿意,可以使用控制器呈现内容:

public class TemplateController : Controller

{

// create a ~/Views/Template/Encoder.cshtml file

public PartialViewResult Encoder()

{

return PartialView();

}

}

然后像@Url.Action一样引用它:

{

....

templateUrl: '@Url.Action("Encoder", "Template")'

}

来自评论

如果您拥有Razor访问权限之外的大部分JavaScript代码(例如外部.js文件),您仍然可以利用Url构建器,只需稍微改变一下即可。例如,我可能会这样做:

public class TemplateController : Controller

{

// Add a child method to the templates controller that outputs default

// configuration settings (and, since it's a child action, we can re-use it)

[ChildActionOnly]

public PartialViewResult Index()

{

// You could build a dynamic IEnumerable model

// here and pass it off, but I'm just going to stick with a static view

return PartialView();

}

}

〜/查看/模板/ Index.cshtml 强>

if (typeof window.App === 'undefined'){

window.App = {};

}

App.Templates = {

Encoder: '@Url.Action("Encoder", "Template")',

Template1: '@Url.Action("Template1", "Template")',

Template2: '@Url.Action("Template2", "Template")'

};

@*

the template files would then reference `App.Templates.Encoder`

when they need access to that template.

*@

@Scripts.Render("~/js/templating")

Index.cshtml(或任何视图)

@* ... *@

@{ Html.RenderAction("Index", "Template"); }

@* ... *@

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值