ASP.Net MVC UserControl in View with different controller? - asp.net-mvc-2

I am trying to create a usercontrol that is an extremely simple form. This usercontrol will appear in a number of different views in my app. I am thoroughly confused on how this can be accomplished.
I have created a controller, and then created a usercontrol that uses that controller.
I then created another controller and created a view for an index of that controller. Inside this view i added my usercontrol reference:
<% Html.RenderPartial("~/Views/UserControlController/Create.ascx"); %>
When i attempt to navigate to the view i can see that the actionresult method for the usercontrol in UserControlController is never called.. What am i missing?? thanks for any help.

Ok, I see your problem. Dont Render partial, you want render action. Whats the difference? Render Partial should read RenderPartialView, not RenderPartialAction. If you want to execute the action, you need Html.RenderAction.

Related

What should a view contain in an MVC?

I am currently working with Swift and I was learning about MVC - one question that popped out was this: I am trying to implement a WKWebView - and I already know how to do this within a ViewController.
My question is: should I create my own WebView class to place the WKWebView or should I only house it with in the ViewController? I am trying to follow the MVC structure.
It's a web view, so it should be placed in the view controller in the storyboard. This really has nothing to do with MVC, per se.
The view controller is your Controller.
The view controller's root view and the web view you're putting in it are the View.
The Model may or may not be relevant here since a model is usually just a data structure that the Controller uses to populate the View. It could just be the HTML that you pass to the web view.
My question is: should I create my own WebView class to place the WKWebView or should I only house it within the ViewController? I am trying to follow the MVC structure.
The view in Model View Controller really refers to a view and its entire graph of subviews. If a WKWebView instance is the view that contains everything that the controller will manage, then it's fine to make that "the" view; there's no need to put it inside another view just for the sake of containing it. On the other hand, if you want the same controller to manage other views not contained in the web view, then you can put the web view and the others all inside some other view and let the controller manage that.
How you organize your views really isn't determined by MVC -- just do what works. MVC really speaks to the way that the information your app operates on is owned and managed by a model, displayed in a view, and how the interactions between model and view are mediated by the controller.

Navigating to a Page versus a View

I am playing around with Xamarin.Forms (mvvm) with Prism and have noticed that some tutorials show navigating to another Page while others show navigating to a View.
At a high level, I understand the literal difference... however I do not understand when I should use one over the other? I have an inclination that some of the reasoning is around dependencies, for example:
A Page has the instance of User > navigate to a view = User is still present when the back operation is used... meanwhile if you want this same behavior in navigating to and from a page, you'll need to pass the instance around via parameters... Is this correct/the reasoning behind navigating to and from views instead of pages?
In X.Forms View's are only visual objects, they do not support any navigation or any kind of infrastructure. They can only be showed when you navigate to a Page that hosts them, it doest not make sense the phrasing navigate to a View. So you should only use Page's.. In your example project they also only navigate to Page's. In the one you say they are navigating to "Views" it is only in the name, because ViewA is a ContentPage
Technically, in pure MVVM, the ViewModel should know completely nothing about the View and vice versa. When you use Page First Navigation approach you violate the first sentence. Here is an example:
class MyViewModel
{
}
class MyView
{
public MyView()
{
InitializeComponent();
// Alternatively you can do the same thing in XAML
this.BindingContext = new MyViewModel();
}
}
As you see the View is aware of the ViewModel.
When you use a ViewModel First Navigation approach, the decision of which
View should have witch ViewModel is delegated to a dedicated class. This class then is used in a custom NavigationService to match a ViewModel to a View. So it will be possible to navigate from a ViewModel to a ViewModel. This way both ViewModel and the View know nothing about each other. The disadvantage of this approach is complexity.
This is a very short answer, however, I hope you will get the key point. There are many examples of both approaches:
ViewModel First Navigation
Page First Navigation
P.S.: Prism has very nice mechanism that handle navigation. What I wrote above and the examples that I provided are just for low level understanding of this approach. If you want to use Prism you definitely have to be familiar with it.

Navigating from view to view in mvc2

Is it possible to navigate directly from a view to another view without having to go down to the controller level?
If by "navigate" you mean to render one view in another, you can use Html.RenderPartial. If you mean creating a link with a URL that will invoke some other view directly when it is clicked, no: it would break the "C" part of MVC.

Clarification of MVVM - interactions between views

I'm using WPF and trying to program the MVVM way.
I understand how every view has its own view model and this works quite well. I am struggling to manage the interaction between views though.
Say I have two views, View1 and View2, each with its own ViewModel, ViewModel1 and ViewModel2. If I have a combobox on View1 and a button, what is the correct way to close the first view, notify the second view of the selection and show the second view once the button is pressed? It doesn't seem like it should go in the model because it's a UI thing. The ViewModel shouldn't know how to open and close WPF forms (or should it?) And the views shouldn't know about any other ViewModels (or should they?)
So how are these problems solved? In a nutshell:
1) How is data passed between views?
2) What manages the lifetime/visibility of views?
It will depend on whether you are doing view model or view first, and the exact implementation details will depend on if you are using an MVVM framework. If you aren't using a framework, then I would strongly recommend you start using one.
In your example, when the button is pressed, a method on ViewModel1 will be invoked. If doing view model first (which I would recommend), you would instantiate an instance of ViewModel2, and at this point you could pass in the combobox selection to the constructor of ViewModel2.
Depending on your framework, there will be different ways of then displaying the view associated with ViewModel2.
For 1) you can sychronize data through the DataModel. Provided each view shares the same instance of the DataModel and it implements INotifyPropertyChanged multiple views can be updated simulateneously.
Your sesond question is a matter of design, as #devdigital states it can depend on whether it is View first or ViewModel first. I would consider the introduction of a Controller class in much as the same way ASP.Net MVC works which controls which view is displayed. You can expose a ViewClosed event on the ViewModel which the controller can listen to and based on your workflow open another view.
You might consider introducing Controllers which are responsible for the lifetime management of the ViewModels. Furthermore, they mediate between the ViewModels.
The sample applications of the WPF Application Framework (WAF) show how these Controllers can be implemented.

MVVM + Datacontext + DataTemplate + Blend = problems

I'm currently using MVVM in a WPF project, all works very well.
I have one Master view and many Detail views that I manage using a currentView property in my MasterViewModel. By using a datatemplate, I bind a view to a viewmodel.
In fact, my master view has a contentcontrol whose content property is binded to my CurrentView property. When I set this currentview property to a viewmodel or another, it calls the corresponding template.
My problem is that using this, my detail views doesn't have explicit datacontext because it is placed by my datatemplate. So in blend, when I open my view to edit its design, I have no datas to bind to my view. If I set a datacontext to my detailview, in blend I can see all datas I can bind but in runtime, the datacontext set by datatemplate is overrided by the datacontext set in my detailview, so I have no datas during runtime.
Does anyone knows how I can create a good MVVM project, with views managed by datatemplates and with datacontext that we can see with Blend ?
Thanks,
I've got a blog post on this issue: http://www.robfe.com/2009/08/design-time-data-in-expression-blend-3/
My post is all about showing data in blend without having to have that data displayed or even created at runtime.
I solved a similar issue in this post:
How can I use Expression Blend to edit a DataTemplate created in Visual Studio?