viewmodel

ASP.net MVC: Creating SelectList in the view or action?

Hey guys I'm just wondering where people are creating their SelectList - in the action or the view. I have seen examples of both and the one that makes the most sense to me is doing it in the action and have the view model have a property of type SelectList. On the other hand, I have seen examples where people have the view model h...

Flat vs Nested ViewModel Classes in ASP.NET MVC

I'm looking for some opinions on two different approaches to ViewModel definition I have a Company class public class Company { public string Name { get; set; } public int CountryID { get; set; } } For the Create and Edit views I need a list of Countries to populate a DropDownList for CountryID selection. I can see two broad ...

ViewModel communication question

Imagine I have a UserControl that shows a parking lot (my favorite analogy) with cars of different colors. You can select a car, and in a separate UserControl (in a separate project) statistics of the selected car are displayed. Now a user wants a button on the car statistics UC, 'Next car of same color'. When selected it should show ...

Uneditable ViewModel properties in a edit view

In my ASP.NET MVC2 application, I have a ViewModel class called UserCreateViewModel. In this class, there are a number of properties that directly map to a LINQ-to-SQL class, called User. I'm using AutoMapper to perform this mapping and it works fine. In my Create action of the UserController, I receive a partially complete UserCreateV...

Validation not working on ViewModel

Ok, I have a ViewModel that looks like this: public class UserLogin { [Required] public string EmailAddress { get; set; } [Required] public string Password { get; set; } } My controller looks like this: [HttpPost] public ActionResult LogIn(UserLogin model) { if (!ModelState.IsValid) { // ... } } ...

MVVM/ViewModels and handling Authorization

Hey guys Just wondering how how people handle Authorization when using MVVM and/or View Models. If I wasn't using VM's I would be passing back the Model and it would have a property which I could check if a user can edit a given object/property but when using MVVM I am disconnecting myself from the business object... and thus doen't ...

Struggling with SelectList in ASP.NET MVC 2

I have a model that looks something like this: public class SampleModel { public static SampleModel Create() { return new SampleModel { Boolean = true, // set several more properties... Colors = new SelectList(new[] { "Red", "Green", "Blue" }, "Green") }; } pub...

How to return different view, but presererve ViewModel in OnActionExecuting

Hi! I'm trying to return a different view if a condition is met. I want to preserve the Model passed into the view from the action. protected override void OnActionExecuting(ActionExecutingContext filterContext) { var subAction = filterContext.RequestContext.RouteData.Values["subaction"].ToString(); var action ...

Does 1 ViewModel really have 1 View?

Hello, its said that 1 ViewModel has 1 View. 1 View is for me a UserControl. What if my UserControl has different areas filled with data from different entities, do I have then several Views and need to build several ViewModels? e.g: I display in a UserControl 3 entities: customer(listbox),order(datagrid),product(datagrid). Each of th...

TreeView using Hierarchical Data Templates binding to different collections

I'm using a TreeView with a Hierarchical Data Template to bind to a View Model hierarchy, my problem is i have multiple child collections of different types (same base class though). Seems relatively simple to use the template to bind one of the collections but i'm struggling to work out how to do both. class ParentViewModel { List<...

Hou to bind uploaded files to IEnumerable<HttpPostedFileBase> property

This the html form: <input id="picture1" name="Input_Pictures" type="file" value="" /> <input id="picture2" name="Input_Pictures" type="file" value="" /> <input id="picture3" name="Input_Pictures" type="file" value="" /> I also tried: <input id="picture1" name="Input_Pictures[]" type="file" value="" /> <input id="picture2" name="Inpu...

ASP.NET MVC 2 - Html.DropDownListFor confusion with ViewModel

I'm getting totally lost and confused on how to use the new strongly typed Html.DropDownListFor helper on ASP.NET MVC 2.0 R2 In the View I'm writting: <%= Html.DropDownListFor(m => m.ParentCategory, new SelectList(Model.Categories, "CategoryId", "Name", Model.ParentCategory), "[ None ]")%> <%= Html.ValidationMessageFor(m => m.ParentCa...

Is it Possible to apply a DataTemplate to a Page?

I'm trying to follow the MVVM pattern laid out here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090097 I have this in my MainWindowResources.xaml file: <DataTemplate DataType="{x:Type vm:VendorsViewModel}"> <vw:Vendors/> <--- I get a "Can't put a page in a style" error in blend with this </DataTemplate> and I'...

MVC Partials/Controls Requiring Data

What is the recommended "cleanest" way to manage a partial that appears on many views and also requires a viewmodel (assume it needs to get some data from a DB). ...

How can the ViewModel drive a ControlTemplate?

I have the problem that parts of the TreeView Controltemplate need to change depending on the state of the bound (ItemsSource) ViewModels. For example the little expander icon needs to be exchanged with a different drawing based on each items ViewModel state. Additonally based on the state of each ViewModel the child items need to be arr...

name 'html' nor name 'model' exist in current context in usercontrol MVC and C#

I am using Microsoft MVC and C#. I have a usercontrol (example.ascx) created and at the top I'm inheriting System.Web.MVC.ViewUserControl<PostTransferViewModel> Now, while my model name is appended to ViewUserControl, I get "The name 'Model' does not exist in the current context" and "The name 'Html' does not exist in the current contex...

ASP.NET MVC - Strongly typed view model, where does it belong?

I'm trying to create a strongly typed view model as John Sheehan suggests here. Where should it go? I can make arguments to myself for Model, View, and Controller. ...

How do I apply a ViewModel to the UserControl inside of a Page?

Doing something like this: <DataTemplate DataType="{x:Type vm:AllCustomersViewModel}"> <vw:AllCustomersView /> </DataTemplate> works in a ResourceDictionary for when I want to apply a ViewModel to a UserControl as root, but how do I the same thing when I have a UserControl inside of a Page? Would I create a ResourceDictionary for all ...

Fetching related data from database via sqldatareader and push into ViewModel/ViewModelCollections

Hello all, Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View with the CustomerOrderProductViewModel.cs: I have several controls like listbox,datagrid,etc.. to display those entities in my View. Those Entities shall be fetched sort of eager loading. That would mean I have 3 sqldatareader in ...

Representing files and lines in a file in model view?

Hi, I am trying to come up with a model design to solve the follwoing problem: I have files (plain text and xml) which I want to be able to represrnt them in a view in ASP.NET, also, I would need to detect certain words in lines and keep track of them. So, we can imagine I have the following CFile: List<Message> CertainMessages M...