Knockoutjs and ASP.NET MVC 3 Helpers

833 views
Skip to first unread message

Foxandxss

unread,
Aug 12, 2011, 1:41:09 PM8/12/11
to KnockoutJS
Hello,

Is there a way to use ASP.NET MVC 3 helpers inside a template?
(jquery.tmpl)

If I want to use a ActionLink and I want to pass a parameter that is a
property of the current item, how I do that?

p.e.

@Html.ActionLink("${Title}", "Action", new { controller =
"Controller", id = ???? })</h2>

I can use the "Title" property of the item but I need to use a
property to pass it as an ID. Something like id = ${id} doesn't work.
(id = "${id}" doesnt work too).

If templates are limited to html, that would be a problem.

Thanks.

emmanu...@gmail.com

unread,
Aug 12, 2011, 2:05:22 PM8/12/11
to knock...@googlegroups.com
I'm a bit confused as to what your question is? Can you elaborate more on what you are trying to do?

Foxandxss

unread,
Aug 12, 2011, 2:29:48 PM8/12/11
to KnockoutJS
Yup yup.

Imagine a template through uhm, Books. So I have access to a book item
inside the template.

Inside that template I want to use a Html.ActionLink from MVC 3.

I want that ActionLink to call an action from a controller and I want
to pass a parameter, the book's Id.

The question is: How I can assign the Id parameter of the book to the
ActionLink?

On a normal MVC 3 foreach, I can do:

@foreach(var book in books)
{
Html.ActionLink("Name", "Action", new { controller = "controller,
id = book.Id})
}

But inside a template I don't know how to assign the book's Id.

I can assign a text to an input using data-bind="text: Title", I can
print my title using ${title} but how I can assign Title or Id to the
ActionLink?

Better? :)
Message has been deleted

Stacey Thornton

unread,
Aug 12, 2011, 3:27:06 PM8/12/11
to KnockoutJS

Foxandxss

unread,
Aug 12, 2011, 3:44:45 PM8/12/11
to KnockoutJS
Or Im not able to explain myself or I don't see what you're trying to
say to me Stacey. Im not using a foreach (or that is not the post I
have to see in the thread?)

On Aug 12, 9:08 pm, Stacey <belldandy.asg...@gmail.com> wrote:
> Please see this thread :http://groups.google.com/group/knockoutjs/browse_thread/thread/b6e8d4...
>
> and this thread :http://groups.google.com/group/knockoutjs/browse_thread/thread/c16862...

Stacey Thornton

unread,
Aug 12, 2011, 3:51:20 PM8/12/11
to KnockoutJS
The posts have nothing to do with using the foreach. The include a
link to HTML helpers that you can use to render Knockout templates
with .NET Lambda Expressions in Razor views.

Stacey Thornton

unread,
Aug 12, 2011, 3:53:27 PM8/12/11
to KnockoutJS
You'll have to actually read the threads for a bit more detail, but
everything you are asking for is in there. There is no point in typing
it out all over again. But here are the highlights.

http://groups.google.com/group/knockoutjs/msg/99074454a7c834d3

http://groups.google.com/group/knockoutjs/msg/4f8cfc4c5eec41f6

Foxandxss

unread,
Aug 12, 2011, 4:39:39 PM8/12/11
to KnockoutJS
Thanks Stacey but I think I'm maybe too new to knockout & asp.net MVC
to understand both posts.

I thought that using a Html Helper inside knockout templates was
easier but you explain too much things there and I'm more confused
now.

On Aug 12, 9:53 pm, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:

Stacey Thornton

unread,
Aug 12, 2011, 4:42:39 PM8/12/11
to KnockoutJS
If you just want to use standard MVC Helpers, You'll have to add the
databinding manually by passing it through the htmlAttributes
parameter, like this.

@Html.TextBoxFor( model => model, null, new { @data_bind : "value:
Name" })

Otherwise, there is no possible way to use the MVC helpers with
Knockout, unless you go with an expressive notation like I linked you
to. You cannot get around mixing HTML and Helpers. You will have to do
it.

Foxandxss

unread,
Aug 12, 2011, 5:02:30 PM8/12/11
to KnockoutJS
Ok, that's more clear. I will find a way to use <a> tags instead of
action links on the templates without messing in the urls.

Thanks.

On Aug 12, 10:42 pm, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:

Stacey Thornton

unread,
Aug 12, 2011, 5:05:39 PM8/12/11
to KnockoutJS
If the only attribute you want is the hyperlink, then you can use the
standard @Url helper.

<a href="@Url('~/you can do path relative stuff here')">

but that doesn't accomplish much.

Foxandxss

unread,
Aug 12, 2011, 5:20:02 PM8/12/11
to KnockoutJS
I just want to create urls (that respect virtual dirs so they are
correct in local and remote) and pass them some parameters from the
template.

If I use something like...

<a href="/controller/action/" + ${id}></a> (I don't know if this
works, is just the idea)

if my app is in:

domain.com/vdir/

the url will be

domain.com/controller/action?parameters

instead of

domain.com/vdir/controller/action?parameters

That's why the ActionLink :P

On Aug 12, 11:05 pm, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:

Stacey Thornton

unread,
Aug 12, 2011, 5:25:11 PM8/12/11
to KnockoutJS
You can do that with the action links, yes.

@Html.ActionLink(text, controller, new { area = "vdir", action =
"action" }, new { @data_bind = "knockout databinding can go here" })

I don't think 'vdir' is an MVC area in your case, but if you can route
to it, you can use it in the ActionLink syntax in some manner. I do
this in my projects all the time.

Stacey Thornton

unread,
Aug 12, 2011, 5:28:07 PM8/12/11
to KnockoutJS
Though this is really easy using the library I linked you, you could
just do ..


@Html.Tag( "a", new tag => {
tag.Attribute("href", String.Format("{0}/{1}", "some url", "$
{id}"));
});

This isn't quite verbatim, but the general idea might work. I've never
tried to use template parameters inside of an expression, but it
renders to the output stream just like the normal view engine, so in
theory it might work. I wouldn't stake my reputation on this comment
though.

On Aug 12, 4:25 pm, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:

Stacey Thornton

unread,
Aug 12, 2011, 5:30:19 PM8/12/11
to KnockoutJS
In all truth, I think this may be less about MVC and more about
Knockout. Have you explored the Knockout Address plugin?

http://learn.knockoutjs.com/#/?tutorial=webmail

Scroll to step 4 and take a look at that. I think this may be more
suitable for you than what I am recommending.

Foxandxss

unread,
Aug 12, 2011, 6:14:58 PM8/12/11
to KnockoutJS
This seems to be good. I copied your classes to my project but Im
getting an error:

@Html.Tag("a", new tag =>
{
tag.Attribute("href", string.Format("{0}", "www.google.com"));
});

But it says:

Compiler Error Message: CS1526: A new expression requires (), [], or
{} after type

and VS mark tag as invalid. So is the syntax correct or Im missing
more classes? (I copied HtmlTag and HtmlTagExtensions)

On Aug 12, 11:28 pm, Stacey Thornton <stacey.cielia.l...@gmail.com>

Stacey Thornton

unread,
Aug 13, 2011, 8:23:28 AM8/13/11
to KnockoutJS
Try this instead.

@Html.Tag(tag =>
{
tag.Is("a")

Foxandxss

unread,
Aug 13, 2011, 1:00:49 PM8/13/11
to KnockoutJS
I ended with an easy solution, but I will consider your options in a
complex scenario.

<a href="@Url.Content("~/Post/IndividualPost/${Id}")">${Title}</a>

Its weird how Url.Content can translate ${id} to the Id and ActionLink
cannot.

Anyway, thank you very much Stacey.

On Aug 13, 2:23 pm, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages