Using Model with none MVC application - asp.net

I'm trying to use a Model with my Asp.net page. But im not using MVC. I get an error when trying to inherit the model from a customcontrol. The error is
ViewModel: interface name expected.
public partial class CustomControl : UserControl, ViewModel

You cant do multiple inheritance in C#. UserControl and ViewModel are both classes and you are only able to inherit from a single class.
You can however implement as many interfaces as you like.

Related

How to pass INavigationService from parent ContentPage ViewModel to child ContentView ViewModel?

I am using Xamarin Forms and Prism. My navigation between pages is done using Prism INavigationService. I have a Xamarin Forms ContentPage with many child ContentViews which have their own View Models. When I click a child ContentView I would like to navigate to another ContentPage with some properties from child ContentView. I want to know if its possible to pass the parent view model INavigationService to child ContentView view model ? Or Is there any other best practices out there which can be used to suit my requirement ?
What you need to do is to register the Page in your prismApplcation which is useually your App.xaml
Once you register your page using RegisterForNavigation< Page> then you can inject the INavigationService in the page's Constructor like this
private INavigationService _navigationService;
public MyPage(INavigationService navigationService)
{
_navigationService = navigationService
}
Alternativly you can do the same thing (which is recommended) in the ViewModel of the page , but you need to make sure to turn on the Autolocater in the Xaml file of the page.
This is the case if you want to navigate between content pages. However, in your case you have multi views and each view has its own viewmodel, although that is kind of breaking the rules since the views can basically share the viewmodel from their page, but if you insist in having it this way, my only advice, without looking at your code, is to register the viewmodels for each view using the same container you registered the page with. Use RegisterType<>() for that and you will find that Inavigationservice will be injected.
Once again I think it is better that you would change your code design.
At the moment the only workaround working for me is I have disconnected auto wiring for child view models. And I have a created all my child view models as member variables in my content page view model and bind them to the views in the XAML. It is working for now. I am not sure this the best practice. If somebody find has a better solution please let me know.

How to Inherit a Page into a Class in ASP.Net?

I have created a class in asp.net and I want to inherit the Main Page. I have tried below but it has an error:
Public Class Class1
Inherits Main Page
End Class
In windows Application inheriting form is much easier than in asp.net. Just like below:
Public Class Class1
Inherits FormMain
End Class
Can someone help me on inheriting page in a class?
Thanks in advance.
Your class name must be a single word. Try Main_Page or MainPage since in many cases the underscore character is hidden by other elements or the cursor.
Otherwise, search you project for the class and make sure you are using the same name as the actual implementation of the class.
This is probably not something you want to really do. The web page in ASP.NET WebForms consists of two parts - Design and Code-behind. The design part is translated into rendering Visual Basic code during compilation.
I can't see a reason for deriving from a WebForms page. What are you trying to achieve? You can certainly change the base class the page uses, but there is practically no logical reason to derive from it.
EDIT:
You might be actually trying to do something in the sense of having a base page and then other pages that have the same basic "structure". That is possible, but using a different ASP.NET mechanism called MasterPages (http://msdn.microsoft.com/en-us/library/wtxbf3hh.ASPX)
Assuming your MainPage is your inheriting class, then make sure it's declared something like:
''' <summary>
''' Base page for all other web pages in the system.
''' </summary>
Public Class MainPage
Inherits Page
...
End Class
Your ASP.NET class would then be:
Public Class Class1
Inherits MainPage
...
End Class

mvc render partial view without httpContext or controller context

Is it possible to render partial view without ViewContext or ControllerContext?
I'm trying to get PartialView Html as string from outside of Controller Action.
OR
Is it possible to call Controller Action from another method? (this will allow me to execute controller action and get partial view html this way).
You can try the Razor Generator Visual Studio Extension. Basically you run the custom tool on your view and it will generate a class that you can pass a model into and it will generate a string.

HtmlHelper class in System.Web.Webpages.Html vs System.Web.Mvc namespaces

In ASP.NET MVC3 I'm trying to set the css class that the validation error helper method sets. (Not this question, the accepted answer just ADDS the class, I want to override it completely.) In looking at the MVC3 source I've found the ValidationInputCssClassName property on the HtmlHelper class. It is settable and it stores the value in a storage provider if set. The get method for the property returns [the provider value] ?? [the default class name].
Now if you just type in HtmlHelper.ValidationInputCssClassName (MSDN entry) in your controller code you'll see that it is a static readonly field. The reason for this is that there are two HtmlHelper classes, one is in the System.Web.Mvc namespace, and the other is in the System.Web.Webpages.Html namespace. The property System.Web.Webpages.Html.HtmlHelper.ValidationInputCssClassName (MSDN entry) can be set, but it doesn't seem to have any effect the generated code no matter where I set it.
What am I missing? And what is the difference between these classes?
Looking at the namespace description, it appears that one of them is designed for Razor and is intended to be used with WebMatrix.
Reference: http://msdn.microsoft.com/en-us/library/gg549171(v=vs.99).aspx
Also:
The System.Web.Mvc namespace contains classes that are used to create HTML elements.
The types in this namespace are in the System.Web.WebPages assembly and are identical to the equivalent types in the System.Web.Mvc assembly.
Reference: http://msdn.microsoft.com/en-us/library/system.web.mvc(v=vs.99).aspx
I'm not positive but I think System.Web.WebPages.Html is for using ASP.net WebForms with Razor.
Since ValidationInputCssClassName, etc. are all readonly fields, I think the only way you are going to get around this is by creating your own HtmlHelper extension methods so you can customize this behavior.
From MSDN:
The System.Web.WebPages namespace contains core classes that are used
to render and execute pages that are built using ASP.NET Web Pages
with the Razor syntax.

Base page in MVC 2

I have just moved over to using ASP.NET MVC 2. In web forms, I normally had a BasePage class that extends from System.Web.UI.Page. And then every page extends from this BasePage. In this BasePage class I have methods that I need. How would I do this in an MVC application?
Any samples would be appreciated.
Thanks.
It is a bit different in MVC. The equivallent would be BaseController although this doesn't correlate exactly to a page in the classic ASP.NET sense. For a start, a controleler doesn't have any markup.
Into a base controller you might inject any model classes that are required by all pages and any common behaviours that have to be executed as part of all Action requests. An example might be some custom checks to go into the Controller OnActionExecuting event...
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onactionexecuting.aspx
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//check the filterContext for a certain condition
if (condition) {
//do something else - redirect to a different route or
//render a different view to to the default
filterContext.Result = new RedirectResult(newUrl);
}
//Otherwise, do nothing, the requested Action will execute as normal...
}
IN MVC there is a greater separation of concerns for rendering the UI, so depending on what the code did in your base page will dictate where it goes in MVC.
If your code generated HTML than you will probably be creating custom HTML helpers and reusable partials views (.ascx). If it handled input data than it will go in a model binder class, and you can create a base model binder for common code. If it talked to your services and domain model than it will go in the controller, and again you can use a base controller. Queries to the persistence layer will go in your model, and reusing code here leads to a much larger discussion of your architecture.
We also moved from base Page classes in ASP.NET and found that a combination of a base controller and a base Model (ViewData) class works well.
So ex Page properties eg: CurrentUser are available from the base Controller and also passed to the base ViewData when its initiated so you can use them on the aspx page.

Resources