Reputation: 33098
Suppose I have index.cshtml
@{
ViewBag.Title = "Index";
}
@model SomeModel
@section JS{
content1
content2
content3
}
<div> View Content </div>
Is there any way I could have a controller action that would serve ONLY the section JS for a request for index.js
?
Such that navigating to http://somesite/index.js
would return
content1
content2
content3
Edit: Some further thoughts on this. My goal would be along the lines say creating a layout page that requires a JS section programmatically, and then composing the View to that layout page and then returning the results of this.
Psuedo code example:
var layout = new LayoutPage();
layout.DefineSection("JS", required: true);
layout.Compose(View("index"))
return layout;
I'm not set on achieving that with what I described but I feel that might offer some more insight on what I'd like to achieve.
Upvotes: 1
Views: 278
Reputation: 26689
You can do something like this (but you'd have to write your own controller action to do it properly)
@model MvcApplication1.Models.TestModel
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
@section JS {
blahblahblah
}
@RenderSection("JS")
public ActionResult Index() {
return View("Index", "_JSLayout", yourModel);
}
This will output only the JS
section. If you want to do it programatically then it will take a bit.
Upvotes: 1
Reputation: 851
The only way to do it would be to make the content of that section into a partial view, and return the partial view from the controller action.
Upvotes: 0