viewmodel

asp.net mvc models vs entity framework models

梦想的初衷 提交于 2019-12-03 11:52:56
问题 Would it be best practice to create a model in your asp.net mvc - model folder. Use these models with your views and by using a service layer to "adapt" my model to the EF-model. Or have you used another approach. The problem with this approachs is that most of the times my (selfmade)model is a copy of the EF-model (not dry) So can someone explain me what models to use with your view cause it is becomming very confusing. model / viewmodel / Entityframeworkmodel .... Solution : Thanks for

Need to pass initial viewmodel data from ASP.NET MVC to knockout.js

≯℡__Kan透↙ 提交于 2019-12-03 11:04:25
问题 I was looking at the Contacts editor sample on the knockout.js website: http://knockoutjs.com/examples/contactsEditor.html The sample works perfectly, but I needed to make two changes to it: Pass the initial data from ASP.NET MVC 3 controller action method. Here is the code from the server: Classes public class Phone { public string Type { get; set; } public string Number { get; set; } } public class Person { public string FirstName { get; set; } public string LastName { get; set; } public

ASP.NET MVC 2 - ViewModel Prefix

社会主义新天地 提交于 2019-12-03 11:04:22
I want to use RenderPartial twice in my view with different models associated. The problem is that some properties are present in both models (nickname, password). They have no prefix, so even the id's or names are equal in the output. Now, if I have model errors for nickname or password, both fields get highlighted. Main View: <div> <% Html.RenderPartial("Register", Model.RegisterModel); %> </div> <div> <% Html.RenderPartial("Login", Model.LoginModel); %> </div> Login PartialView: <% using (Html.BeginForm("Login", "Member")) { %> <fieldset> <legend>Login</legend> <p> <%= Html.LabelFor(x => x

ASP.NET MVC ViewModel with methods - is it “legal”?

▼魔方 西西 提交于 2019-12-03 09:41:49
Should viewmodels be limited to only have properties, and not methods? Let's say I have a radio button in my view, and wants to see if the radio button should be checked. I could do this entirely in my view: @Html.RadioButton("radiobuttonName", "The value", (id == Model.PersonId)) or I could move this logic into the viewmodel: @Html.RadioButton("radiobuttonName", "The value", Model.IsChecked(id) using this method in the viewmodel: public int PersonId { get;set;} public bool IsChecked(int id) { return (id == PersonId); } Is this OK to do, or should it be done entirely in the view, or in some

MVC Dynamic View Data and Dynamic Views

回眸只為那壹抹淺笑 提交于 2019-12-03 08:51:54
问题 Traditionally, I have built MVC applications using view models with Data Annotations attributes, and I dynamically render the views using editor templates. Everything works great, and it really cuts down on the time it takes me to build new views. My requirements have recently changed. Now, I can't define the view model at design time. The properties that will be rendered on the view are decided at run time based on business rules. Also, the validation rules for those properties may be

Where do you put your validation in asp.net mvc 3?

守給你的承諾、 提交于 2019-12-03 08:46:21
问题 One common recommended practice in asp.net mvc is that you should not send your business models to your views.. instead you should create viewmodels specific to each view. When that is done and you call the ModelState.IsValid method in your controller you are effectively checking the validity of the viewmodel but not the business object. What is the conventional approach to dealing with this? public class Person { public int ID {get; set;}; [Required] public string Name {get; set;} [Required]

DRY vs Security and Maintainability with MVC and View Models

风流意气都作罢 提交于 2019-12-03 07:41:14
I like to strive for DRY, and obviously it's not always possible. However, I have to scratch my head over a concept that seems pretty common in MVC, that of the "View Model". The View Model is designed to only pass the minimum amount of information to the view, for both security, maintainability, and testing concerns. I get that. It makes sense. However, from a DRY perspective, a View Model is simply duplicating data you already have. The View Model may be temporary, and used only as a DTO, but you're basically maintaing two different versions of the same model which seems to violate the DRY

How to pass data (selected item) to Durandal composed detail view?

安稳与你 提交于 2019-12-03 07:31:51
I've started using (as of, a few hours ago) Durandal with the hope to manage views and allow composition within a single page - the previous approach, also using Knockout, was getting too unwieldy to maintain in a fat HTML file. I've installed/setup Durandal and I can create views and viewmodels - however, I don't know how to get data into the viewmodel to use as a basis for the new viewmodel. For instance, I have a "left nav bar" for selecting items - when an item is selected it updates a "selected item" observable in the current model, but it should also load the correct "detail view" in the

ASP.NET MVC - Should I use the Repository Pattern to write ViewModels to the database or convert them to Models first?

不羁岁月 提交于 2019-12-03 07:28:46
In my ASP.NET MVC app, I have a fairly complex edit page which combines a number of models into one view. I'm using the ViewModel pattern to combine all of this information and present one cohesive object to the View. As an example, my ViewModel structure is something like this: CompanyId CompanyName List<Employee> Employees List<ContactMethod> ContactMethods The Employee object has a number of basic properties, and a preferred contact method. On the edit page, the user is given all of the employees of the company and they have the ability to add and remove (using javascript), as well as edit

Select() Input Field with Knockout.js

老子叫甜甜 提交于 2019-12-03 06:19:00
I am new to Knockout.js. What is the best way to select() an <input /> when it becomes visible? View: <p> Name: <b data-bind="visible: !editing(), text: name, click: edit"> </b> <input data-bind="visible: editing, value: name, hasfocus: editing" /> </p> ViewModel: function PersonViewModel(name) { // Data this.name = ko.observable(name); this.editing = ko.observable(false); // Behaviors this.edit = function() { this.editing(true) } } ko.applyBindings(new PersonViewModel("Bert Bertington")); http://knockoutjs.com/documentation/hasfocus-binding.html http://jsfiddle.net/RnCUd/ Thanks! You can