In an ASP.MVC application, I have a function defined in my _ViewStart.cshtml like this:
#functions
{
public void Foo()
{
....
}
}
When I try to use this function in of my views, I get an exception saying that :
The name 'Foo' does not exist in the current context
I thought that all what I define in _ViewStart.cshtml is accessible to all my views that define it as layout.
I am missing something here ...
Thank you for your help
You could place reusable functions them inside Razor views of the special App_Code folder.
For example inside ~/App_Code/Foo.cshtml you could declare a Bar function:
#functions {
public static void Bar() {
}
}
that will be accessible from any Razor view:
#{Foo.Bar();}
Also notice that the function must be declared as static.
Related
While writing library documentation I need to be able to reference (i.e. link to) methods from other classes (in the same library) but with the same method name (i.e. reference the delegating method from the docs of the one that is doing the work).
I have tried ClassName.method (does not work) and directly using the method (references the same class method).
Any ideas?
Thanks.
/// [B.someMethod] ..
/// [someMethod] ..
class A {
void someMethod() {
}
}
/// [A.someMethod]
/// [someMethod]
class B {
void someMethod() {
}
}
/// [A.someMethod]
void main() {
new A().someMethod();
}
All references in the doc comments work for me in this example, but sometimes DartEditor shows them only as links after a delay or after some other edits.
Its a docgen issue/bug - can be monitored here: https://code.google.com/p/dart/issues/detail?id=22144
I am busy developing my first non-example Orchard module. It is a handful of controllers and views, with custom (EF) data access, and is largely independent of Orchard content types and parts. Normally I set up mappings in an Application_Start handler, but as the actions in this MVC module will be invoked in the context of the Orchard application, I no longer have that point of entry. My most obvious and immediate solution is to move mapping initialization into static constructors for mapped view models, e.g.
public class ApplicantPersonalDetailsModel : MappedViewModel<Applicant>
{
static ApplicantPersonalDetailsModel()
{
Mapper.CreateMap<Applicant, ApplicantPersonalDetailsModel>().Bidirectional();
}
....
}
How else can I do this? is there a better way to do this in MVC3/4 in general, or preferably, an event or hook I can grab in the Orchard application to also achieve this on applicaion startup?
The way I have done it is by implementing IOrchardShellEvents
public class MenuOrchardShellEvents : IOrchardShellEvents
{
public void Activated()
{
Mapper.CreateMap<YSRB.Menu.Models.Records.Customer, YSRB.Menu.Models.ViewModels.CustomerViewModel>()
.ForMember(c => c.CustomerType,
m => m.MapFrom(
x => (CustomerTypes)x.CustomerType
)
);
Mapper.CreateMap<YSRB.Menu.Models.ViewModels.CustomerViewModel, YSRB.Menu.Models.Records.Customer>()
.ForMember(c => c.CustomerType,
m => m.MapFrom(
x => (int)x.CustomerType
)
);
}
public void Terminating()
{
//Do nothing
}
}
Hope this helps.
The Handler is the best place for initializing your variables, even if you haven't defined any part inside your module you can define one without a driver but with handler.
public class InitPartHandler : ContentHandler
{
public InitPartHandler(IRepository<InitPartRecord> repository)
{
OnInitializing<InitPart>((context, part) =>
// do your initialization here
);
}
}
EDIT
InitPart and InitPartRecord would be
public class InitPart : ContentPart<InitPartRecord>
{
}
public class InitPartRecord : ContentPartRecord
{
}
I have Razor function which outputs some data and as result does not return anything (that's a long story why it is done this way):
#functions
{
public static void SampleHelperMethod()
{
//...
}
}
How can I call it in view now? I tried #MyFunctions.SampleHelperMethod() but it doesn't work for void functions.
Declaration
#functions
{
public static void TestFunction()
{
}
}
Use in View
#{ TestFunction(); }
Because this is a function that does not return anything, you need to wrap it in the braces like you would and if/for statement. However, like Erik said, it is really unclear why this logic would be declared in the view...you may consider creating a helpers class that your views can include. This will allow for reuse and better separations of concerns.
Please have a look at below given code.
<?php
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// Foo::testPublic
?>
In above example, when we called $myFoo->test();it called testPrivate of Bar class
But how come it called testPublic of Foo class.
Can any one help me in this ?
Bar.testPrivate and Foo.testPrivate have to be protected methods instead of private ones. See here for more:
http://php.net/manual/en/language.oop5.visibility.php
Because test() is NOT in Foo and is running in Bar scope. Bar scope can't access to Foo private methods.
Just add test() to Foo...
Indeed one of the comments on the visibility page does reiterate this:
"private methods never participate in the in the overriding because these methods are not visible in the child classes."
It does feel a bit strange because you would think that the child class would override the parent with the method names being the same, but its not the case with private methods and the parents method takes precidence here, so best to use protected methods if you want to override.
I have an issue with some compiler warnings I am getting when using T4 MVC (along with MVC3 RC2).
Say you have the following classes….
public partial class ParentController { }
public partial class ChildController : ParentController { }
T4MVC will generate something like…
public partial class ParentController
{
[GeneratedCode("T4MVC", "2.0")]
public readonly string Name = "Parent";
}
public partial class ChildController
{
[GeneratedCode("T4MVC", "2.0")]
public readonly string Name = "Child";
}
Which causes a compiler warning to occur suggesting the use of the ‘new’ keyword on the Name property in the derived class.
Is there something that can be done (short of turning the warning off completely) to get around this issue?
You could always drag the T4 templates into your own project and customize them to your needs.