viewmodel

Populate DropDownList in ASP.NET MVC from database table using Entity Framework 6 and ViewModel

强颜欢笑 提交于 2019-12-04 10:17:17
I have been scratching my head for a whole night on an issue I can do quickly using ajax/jquery and stored procedures. I want to 1) Populate a drop down list from values obtained from a database table using Entity Framework and view model. I DO NOT WANT TO USE VIEWBAG OR VIEWDATA. Any help appreciated. 2) How can I generate a Create View using the View Model with the all the default fields ? The scaffholding works on a model but not on a view model ? MY MODELS public class Employee { public int EmployeeID { get; set; } public string Name { get; set; } public string Gender { get; set; } public

Should my viewmodel value type properties be nullable?

巧了我就是萌 提交于 2019-12-04 09:36:33
I've been making my viewmodel properties nullable for quite some time now. My reasoning is that when a validation occurs, I don't want default values inserted into the fields that the user left empty, but are required. I mark my required fields with required, for course, but this got me to thinking that i'm losing a great deal of fidelity in the object model by doing this. Of course my domain classes are only nullable when they can actually be null. Should my viewmodel properties be nullable when the domain model requires them? Should my viewmodel properties be nullable when the domain model

Popup using knockout js

亡梦爱人 提交于 2019-12-04 08:22:24
i'm migrating one of my older jquery plugins from DOM jungle to this fancy mvvm framework knockout. Which technique would i use to properly display a popup container? I ahve to populate it 'by call' since i get a json feed every time. I tried an approach using the with binding, but it still attempts to populate the partial at its first runtime. <!-- ko with: daySubmitFormViewModel --> <div class="ec-consulation-lightbox"> <form id="cForm" class="form-container"> // Some bindings here. </form> </div> <!-- /ko with: --> It can be done without custom binding as well. Example is below <div class=

Knockout.js: Multiple ViewModel bindings on a page or a part of a page

不羁岁月 提交于 2019-12-04 08:11:22
问题 I am wondering if it is possible to use Knockout.js 's ko.applyBindings() multiple times to bind different ViewModels to one part of a page. For example, let's say I had this: <div id="foo">...</div> ... ko.applyBindings(new PageViewModel()); ko.applyBindings(new PartialViewModel(), $('#foo')[0]); I am now applying two ViewModel bindings to <div id="foo> . Is this legal? 回答1: You do not want to call ko.applyBindings multiple times on the same elements. Best case, the elements will be doing

ASP.NET MVC Model/ViewModel Validation

℡╲_俬逩灬. 提交于 2019-12-04 08:09:10
I have model classes in Linq-to-Sql with partial classes marked with data annotation attributes and a reference to xVal. When I bind a view directly to a model everything works great, both the JS generated by xVal and server side double check. Many of my views don't take input to one specific model, so I am setting up view model classes. Instead of exposing an entire model instance I expose properties into the model that I allow/need to be set by the view. // foo model public class Foo { public string FooField { ... } public Bar Bar { ... } } // bar model, where bar is a parent relationship of

Effectively avoiding ViewBag in ASP.NET MVC

强颜欢笑 提交于 2019-12-04 07:15:40
How do you deal with avoiding ViewBag due to its risk of error with being dynamic but also avoid having to populate a new ViewModel and pass it back to the view each time. For instance, I don't want to necessarily change the follow to expose common data normally stuffed in ViewBag. [HttpGet] void Index() { return View(); } to [HttpGet] void Index() { var messages = new MessageCollection(); messages.AddError("Uh oh!"); return View(messages); } Where in the pipeline would I add a property like ViewBag that is custom and strongly typed but have it exposed elegantly in the Controller and also the

MVC pass JSON ViewModel to View

痞子三分冷 提交于 2019-12-04 06:57:34
I have an MVC application that I'm using various JsonResult endpoints to populate the javascript ViewModel. I have been using several jQuery Ajax requests to populate the model, but I'd like as much of the inital model to be passed to the view on the server. The ViewModel has 3-5 pieces (depending on where the user is in the application): Basic page links, these don't change very often and could be the exact same throughout the entire user's session User notifications. User data. (optional) Viewable data (optional) misc data I'm currently using this code to load the first three pieces: $

ASP.NET MVC - view model, domain model and data model [closed]

匆匆过客 提交于 2019-12-04 04:41:40
I am using entity framework in my latest ASP.NET MVC 3 project. As it is DB first, the entity framework generates Database models . In my service (business) layer I Scaffold (MvcScaffolding) to generate service methods, views and controllers. Scaffolding also generates the domain models . When binding these models to the Views, I use view models . In short, I ended up in using three types of models. Is this Ok? The view models are kept in the Presentation layer, domain models are kept in the business layer and data models are kept in the repository layer. Please let me know your thoughts. That

MVVM: Binding a ViewModel which takes constructor args to a UserControl

↘锁芯ラ 提交于 2019-12-04 04:40:31
问题 My WPF app has a MainWindow containing a usercontrol called TvshowGridView. MainWindow: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:NevermissClient" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:views="clr-namespace:NevermissClient.Views" x:Class="NevermissClient.MainWindow"

knockout “if binding” not working

陌路散爱 提交于 2019-12-04 04:35:37
When debugging with Chrome, I can see CoverPrices has 9 elements. The foreach loop actually works well and the table looks correct with the first span being bound to Item1 correctly. However, the if binding does not work and both images are displayed. Yet, all the elements in Item2 have the true value, so only the first image should show up. <!-- ko foreach: CoverPrices --> <tr> <td> <span data-bind="text: Item1"></span> </td> <!-- ko foreach: Item2 --> <td> <img src="~/Images/yes.png" alt="oui" data-bind="if: $data" /> <img src="~/Images/no.png" alt="non" data-bind="ifnot: $data" /> </td> <!-