viewmodel

What is the correct way to pass data between view models?

被刻印的时光 ゝ 提交于 2019-12-05 18:14:26
I've just started with MVVM and I have been reading up on it and doing some examples. I've managed to create an application that will read from the database and then populate into a listbox. I am having difficulty in trying to link up the selected item into another view and then do a bit of processing in that views viewModel. Please can somebody explain to me the correct way to get the currently selected item from view1 listbox and then on view2 label just to output the selected item? Here is my XAML: <local:SecondView Margin="499,30,0,20"> <local:SecondView.DataContext> <v:MainViewModel /> <

MVC3 - Insert using ViewModel - Object reference not set to an instance of an object

╄→尐↘猪︶ㄣ 提交于 2019-12-05 15:44:22
问题 I have got two models, seen below, and am trying to insert one of each to the database from one view. I have created a view model in an attempt to do this. public class Blog { public int BlogID { get; set; } public string Title { get; set; } public DateTime CreatedOn { get; set; } public virtual User User { get; set; } public virtual ICollection<BlogPost> Posts { get; set; } } public class BlogPost { public int PostID { get; set; } public string Body { get; set; } public DateTime CreatedOn {

ASP.NET MVC ViewData and view model best practices

℡╲_俬逩灬. 提交于 2019-12-05 15:01:01
The initial situation is that I map my domain model into a presentation model. I have to display an update/create formular with textboxes and a dropdownlist. Should the viewmodel contain a list for the dropdownlist or should I pass the data for the dropdownlist by using ViewData? When should I use ViewData and when should I not use it? Shall input fields like dropdownlists have a seperate view model? I tend to try and use ViewData as little as possible since you always need to cast values, you need to do error checking for nulls or for keys that don't exist and it clutters the views in my

Accessing ViewModel in JS file (asp.net MVC)

为君一笑 提交于 2019-12-05 08:53:05
I have been using something like this inside Razor @section Includes { <script type="text/javascript"> var somestuffneeded = @(Html.Raw(Json.Encode(Model.datamember))); </script> } But this looks not so clean because it goes in the same file as the layout, (since it won't work from the .js file directly). Any clean alternatives to accessing and viewing the ViewModel passed inside .js file? You can't directly access ViewModel in .js file because its static file on your web server. But there is a workaround that you can pass ViewModel to .js file with parameter. some .js File function Common() {

Validating Nested Models

流过昼夜 提交于 2019-12-05 08:34:50
I currently have a ViewModel setup as such: public class OurViewModel { public OurViewModel() { } [Required] public int LeadID { get; set; } [Required] public int Rate { get; set; } [Required] public bool DepositRequired { get; set; } [RequiredIfOtherPropertyIsTrue("DepositRequired")] public BankInfo { get; set; } } ...in this case, "RequiredIfOtherPropertyIsTrue" is a validator that does pretty much what it says: checks to see if another property is true (in this case, our boolean indicating whether or not a deposit is required), and BankInfo is another model that looks something like this:

How do you ignore/persist values in MVC when your view-model doesn't have as many fields as your domain model?

五迷三道 提交于 2019-12-05 07:43:05
I have a site where I'm using fluentNhibernate and Asp.net MVC. I have an Edit view that allows user to edit 8 of the 10 properties for that record (object). When you submit the form and the Model binds, the two un-editable fields come back in the view-model as Empty strings or as default DateTime values depending on the type of property. Because I'm also using AutoMapper to map my view-model to my Domain Entity, I cannot just load a fresh copy of my object from the database and manually set the 2 missing properties. Whats the best way to persist those fields that I don't want edited? One way

When should I be using AutoMapper and when not

旧城冷巷雨未停 提交于 2019-12-05 06:03:33
I have a Data layer which holds my EF6 DbFirst edmx, repositories, and AutoMappings. I also have a Model layer with a Poco for each auto generated entity in my Data layer. The properties pretty much match exactly except for a few name changes. AutoMapper is installed to my DataLayer only and this is where I set all of my mappings in a config file. At this point I have a mapping from each DataLayer entity to each ModelLayer entity and each ModelLayer entity to each DataLayer entity. Any name changes are specified in the mappings. Since it is setup this way in my repository save methods the

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

我的未来我决定 提交于 2019-12-05 06:00:55
问题 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 context. If I removed the <PostTransferViewModel> from the end of ViewUserControl then everything works fine, but I need <PostTransferViewModel> . <%@ Control

Windows Phone 8 - MVVM ViewModels and App.xaml.cs

与世无争的帅哥 提交于 2019-12-05 05:43:26
I've been studying the MVVM pattern and putting it into practice in a Windows Phone 8 app, and I have a question about the best practices for initializing and accessing ViewModels in an app. When I create a Databound Application from the WP8 SDKs templates, I noticed this code in the App.xaml.cs file: public static MainViewModel ViewModel { get { // Delay creation of the view model until necessary if (viewModel == null) viewModel = new MainViewModel(); return viewModel; } } private void Application_Activated(object sender, ActivatedEventArgs e) { // Ensure that application state is restored

ViewModel collection property lost values after posting back to controller action in MVC 3

余生颓废 提交于 2019-12-05 03:30:18
I have my view models : public class POReceiptViewModel { public virtual int PONumber { get; set; } public virtual string VendorCode { get; set; } public virtual IList<POReceiptItemViewModel> POReceiptItems { get; set; } public POReceiptViewModel() { POReceiptItems = new List<POReceiptItemViewModel>(); } } public class POReceiptItemViewModel { public virtual string ItemCode { get; set; } public virtual string ItemDesription { get; set; } public virtual decimal OrderedQuantity { get; set; } public virtual decimal ReceivedQuantity { get; set; } public virtual DateTime ReceivedDate { get; set; }