Declarative AJAX "Controls" in MVC
I want to create a reusable ajax control in MVC .NET using RAZOR.
my example is a simple ajax text box and list where the user filters the list by typing in the text box. on the first call i would render both the text box and the list using my razor view. on subsequent AJAX calls i would want to ONLY render the (now filtered) list.
idea 1: use @if statement to conditionally render code.
problem: razor does not seem to like conditionally written html. for example it er开发者_如何转开发rors when a <div>
tag is not followed by a closing </div>
.
idea 2: use @section
tokens to create portions of my control and then call RenderSection
within the same file as needed.
problem: razor does not allow RenderSection
to call sections in the same page
i know i can conditionally render html as strings, but i wanted to take advantage of the legibility of the razor markup and keep with development protocols.
You should be able to output <div>
tags in a Razor block without the corresponding </div>
tag by surrounding it with <text>
. The reason is that Razor uses the closing tag to know when to drag back into code-parsing mode:
@if (myCondition)
{
<text>
<div>
</text>
}
As for the Section
stuff, you might be able to achieve what you want using Templated Razor Delegates, like this:
@{
Func<dynamic, object> b = @<strong>@item</strong>;
}
// ...
<span>This sentence is @b("In Bold").</span>
See Phil Haack's blog for a little more on this.
精彩评论