viewmodel

Where to create parametrized ViewModel?

感情迁移 提交于 2019-11-29 02:54:09
问题 I have recently parametrized my ViewModel's contructor. Before that, I was doing this in my window: <Window.DataContext> <vm:MyViewModel /> </Window.DataContext> The framework instantiated the ViewModel for me. I know I can set DataContext in code but I would prefer a XAML way so designer can display my test data when designing. Is this possible? 回答1: Use an ObjectDataProvider if you want to specify constructor parameters: <Window.DataContext> <ObjectDataProvider ObjectType="vm:MyViewModel"

MVVM (with WPF) - Binding Multiple Views to the Same ViewModel

扶醉桌前 提交于 2019-11-29 02:35:31
问题 I have recently started investigating the MVVM pattern with WPF for an upcoming project. I started with Josh Smith's MSDN article. I have a question (well many, but let's start with one): I have an IndividualViewModel which exposes the properties of the model. I need two views "Add Individual" and "Edit Individual" which are very similar as you can imagine. What I have done currently is to have 2 subclasses AddIndividualViewModel and EditIndividualViewModel which expose the Add and Edit

ASP.NET MVC - Database entities or ViewModels?

旧时模样 提交于 2019-11-29 02:09:28
问题 I am currently working on an ASP.NET MVC project. Some developers on the team want to bind the auto-generated database entities directly to the Views. Other developers want to create tailor-made ViewModel's and bind those to the Views. Objectively, what are the pros and cons of both approaches? (By "database entities" I am referring to the auto generated classes that an ORM framework generates, such as LINQ to SQL, Entity Framework or LLBLGen). 回答1: Definitely use view models in your views ,

How does DataAnnotationsModelBinder work with custom ViewModels?

。_饼干妹妹 提交于 2019-11-29 00:22:02
I'm trying to use the DataAnnotationsModelBinder in order to use data annotations for server-side validation in ASP.NET MVC. Everything works fine as long as my ViewModel is just a simple class with immediate properties such as public class Foo { public int Bar {get;set;} } However, the DataAnnotationsModelBinder causes a NullReferenceException when trying to use a complex ViewModel , such as public class Foo { public class Baz { public int Bar {get;set;} } public Baz MyBazProperty {get;set;} } This is a big problem for views that render more than one LINQ entity because I really prefer using

MVC3 multi step form - How to persist model object

不羁岁月 提交于 2019-11-28 23:49:22
I have a multi step form which uses one model object and I need to persist it between the steps. The object gets saved to the database only after the final step. I have seen people suggest using HTML.Serialize but how secure is this option? Also my model object will grow as the user fills up the form which means the hidden form field with serialized data will add up size to my HTML output. Whats the best practice for this kind of situation? I've use the TempData for this purpose. You can store an object (a copy of your model data) in TempData, and use it in the next request. If it is not set

MVVM and View/ViewModel hierarchy

前提是你 提交于 2019-11-28 23:02:42
问题 I'm working on making my first game using C# and XAML for Windows 8. I'm still learning the core concepts and best practices, and MVVM has been a hurdle. I'll attempt to ask the question in two parts. Background The game I'm making is Sudoku. Sudoku has a board that contains a 9x9 grid of tiles. I have three models - Game , Board , and Tile . When a Game is created, it automatically creates a Board , and when the Board is created, it creates 81 (9x9) Tiles . 1. With a hierarchy of views, how

How to get data for a dropdownlist into viewmodel when using AutoMapper/AutoMapViewResult

十年热恋 提交于 2019-11-28 21:39:23
After reading ASP.NET MVC 2 in Action and watching Jimmy Bogard's presentation from MvcConf (both highly recommended!), I began to implement some of their ideas. One of the cool things they do, is not only to use AutoMapper to map your entities to some viewmodel, but automate this with an AutoMapViewResult: public class EventsController : BaseController { public ActionResult Show(Event id) // EntityModelBinder gets Event from repository { return AutoMapView<EventsShowModel>(id); // AutoMapView<T>(model) is a helper method on the BaseController, that calls AutoMapViewResult<T>(...) } } // not

Share ViewModel between fragments that are in different Activity

故事扮演 提交于 2019-11-28 21:19:44
I have a ViewModel named SharedViewModel: public class SharedViewModel<T> extends ViewModel { private final MutableLiveData<T> selected = new MutableLiveData<>(); public void select(T item) { selected.setValue(item); } public LiveData<T> getSelected() { return selected; } } I implement it based on SharedViewModel example on the Google's Arch ViewModel reference page: https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing_data_between_fragments It is very common that two or more fragments in an activity need to communicate with each other. This is never trivial as

ASP.Net MVC 3 ViewModel Data Annotations

自作多情 提交于 2019-11-28 21:17:39
问题 I am developing an ASP.Net MVC 3 Web application with Entity Framework 4.1 and I am getting a bit confused with regards using Data Annotations for form validation. I always return a ViewModel to a View as opposed to passing the actual object as I realise this is poor practice. For example: public class ViewModelTeam { public Team Team { get; set; } } My View might then have something like this @model UI.ViewModels.ViewModelTeam @Html.HiddenFor(model => model.Team.teamID) <div class="editor

How to handle custom Properties in AutoMapper

巧了我就是萌 提交于 2019-11-28 21:16:07
I've got a ViewModel that takes some Model data and slightly alters it. The way I'm doing it "works" since I just pass the DomainModel to the constructor for the ViewModel , but since I'm using AutoMapper on some of my one-to-one ViewModels, I thought I'd try and learn how to do the mapping across all ViewModels. Here's an example of a ViewModel that does a little extra. public class UsersDetailsViewModel { public string UserName { get; set; } public string Email { get; set; } public string Website { get; set; } public int ID { get; set; } public List<OpenID> OpenIds { get; set; } public