English 中文(简体)
Knockout.js and MVC
原标题:

Just started playing with knockout.Js which is a fantastic framework Steve s really done well with that one. One thing I can t seem to do at the minute is impliment it with my Html Helpers. So for exmaple I ve got :

 <%: Html.TextBoxFor(model => model.Division) %>

but I d line to use the databind on that but at the minute I cant get the "data-bind" attribute into the helper. I ve used attributes before such as @class, Id etc but this one is tricky due to the - any Ideas.. Ive tried:

<%: Html.TextBoxFor(model => model.SupplierName, new { data-bind = "SupplierName"}) %>

and

 <%: Html.TextBoxFor(model => model.SupplierName, new { "data-bind"" = "SupplierName"}) %>

but no joy. we heavily use the Editor and Text box helpers and I d really like to integrate these into the Item s with knock out..

Any help much appretiated

最佳回答

This should work:

<%: Html.TextBoxFor(model => model.SupplierName, new { data_bind = "SupplierName"}) %>

Variable names cannot contain a hyphen (-) but if you use an underscore (_) in an HTML attribute, it is automatically converted to a hyphen when its rendered .

问题回答

You can supply attributes either as anonymous object or as a dictionary. In this particular case a dictionary should be used:

<%: Html.TextBoxFor(m => m.SupplierName, new Dictionary<string, object> { { "data-bind", "SupplierName" } }) %>

I used Jim s answer as the base for my MVC 4 solution.

Jim s:

<%: Html.TextBoxFor(model => model.SupplierName, new { data_bind = "SupplierName"}) %>

Mine:

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

See Using Html.TextBoxFor with class and custom property (MVC) for how to do it in MVC3. Im using MVC2 so the above answer was spot on for me





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签