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"); }

@* ... *@


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部