Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout MVC - VB.NET issue with IsAssignableFrom and MethodCallExpression
    primarykey
    data
    text
    <p>I am having problems with a library called Knockout MVC. It is a .NET wrapper around Knockout.js. While it works pretty good when using in a C# based ASP.NET MVC project, it simply doesn't in a VB.NET based project and I found out why.</p> <pre><code>protected virtual string VisitMethodCall(MethodCallExpression m) { ... if (typeof(Expression).IsAssignableFrom(m.Method.ReturnType)) return VisitMemberAccess(m.Object, m.Method.Name); throw new NotSupportedException(); } </code></pre> <p>The condition always evaluates to False when the project that calls the library is written in VB.NET. Same thing in C#: working perfectly.</p> <p>Since I am not quite sure what is causing this different behaviour, I thought it might be a good idea to ask the community. Maybe someone knows how this line could be translated into something that works with both C# and VB.NET.</p> <p>The code can be found at ~ line 260 <a href="https://github.com/AndreyAkinshin/knockout-mvc/blob/master/PerpetuumSoft.Knockout/Utilities/KnockoutExpressionConverter.cs" rel="nofollow">here</a>.</p> <p><strong>[EDIT]</strong></p> <p>To clarify things: The code from above does not need to be translated to VB.NET. The problem is rather, that the condition it contains returns different results based on whether it is called from C# or VB.NET code. I believe that IsAssignableFrom behaves differently for VB.NET and C#... so I need to know how to get this condition to work independent of what language it is called from.</p> <p><strong>[EDIT] - Example</strong> The following steps reproduce the problem:</p> <ol> <li>Create a new ASP.NET MVC 4 project in Visual Studio 2012 using VB.NET</li> <li>Add Knockout MVC via Package Manager Console (Install-Package kMVC)</li> <li><p>Ignore the error (Methods is null), it is a NuGet Script error, and add the following line to Global.asax.vb (Application_Start) manually:</p> <p>ModelBinders.Binders.DefaultBinder = New PerpetuumSoft.Knockout.KnockoutModelBinder()</p></li> <li><p>Add the following configuration to the BundleConfig.vb </p> <p>bundles.Add(New ScriptBundle("~/bundles/knockout").Include( "~/Scripts/knockout-{version}.js", "~/Scripts/knockout.mapping-latest.js", "~/Scripts/perpetuum.knockout.js"))</p></li> <li><p>Render the ScriptBundle in _layout.vbhtml beneath the modernizer </p> <p>@Scripts.Render("~/bundles/knockout")</p></li> <li><p>Add a new model to the Models folder </p> <p>Imports DelegateDecompiler</p> <p>Public Class HelloWorldModel</p> <pre><code> Public Property Firstname As String Public Property Lastname As String &lt;Computed&gt; Public ReadOnly Property Fullname As String Get Return Firstname + " " + Lastname End Get End Property </code></pre> <p>End Class</p></li> <li><p>Add a new controller to the Controllers folder </p> <p>Public Class HelloWorldController Inherits PerpetuumSoft.Knockout.KnockoutController</p> <pre><code> ' ' GET: /HelloWorld Function Index() As ActionResult Return View(New HelloWorldModel With { .Firstname = "John", .Lastname = "Doe"}) End Function </code></pre> <p>End Class</p></li> <li><p>Compile the project so that the MVC extension gets to know the model</p></li> <li>Right click the Index action in the controller and select "Add View.."</li> <li>Leave the View name and choose Create a strongly-typed view, selecting HelloWorldModel as model; leave the rest with default options and click Add</li> <li><p>Add an Imports statement to the top of the view </p> <p>@Imports PerpetuumSoft.Knockout </p></li> <li><p>Create the Knockout Context in the Code Section </p> <p>Dim ko = Html.CreateKnockoutContext()</p></li> <li><p>Define the view and apply the model under the h2 tag </p> <p>&lt;p>First name: @ko.Html.TextBox(Function(m) m.Firstname)&lt;/p><br/> &lt;p>Last name: @ko.Html.TextBox(Function(m) m.Lastname)&lt;/p><br/> &lt;h2>Hello, @ko.Html.Span(Function(m) m.Fullname)!&lt;/h2><br/></p> <p>@ko.Apply(Model)</p></li> </ol> <p>Now if you compile and execute the application and call the HelloWorld controller (localhost:nnnnn/HelloWorld) then you will get the NotImplementedException raised on the line ko.Apply(Model).</p> <p>If you repeat the steps from above when creating an ASP.NET MVC project with C#, everything works as expected.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload