I'm new to .Net development, and now are following NerdDinner tutorial. Just wondering if any of you would be able to tell me What is the differences between ViewData and ViewModel(all I know is they are used to pass some form of data from controller to view) and perhaps tell me on what situation should I use ViewData instead of ViewModel and vice versa
Note: I will use the term "ViewModel" for both the ViewModel in WPF/Silverlight and the strongly-typed ViewData in ASP.Net MVC in the following text.
I would like to create both ASP.Net MVC and WPF/Silverlight clients for the same project (in other words, against the same DataModel), should I create a common ViewModel project or a separate ViewModel for each client technology?
I would like to believe a common ViewModel is the right thing to do, but the need to create Dependent Properties or JSON strings make it seem to be incompatible.
Maybe another solution is to put the common part of the ViewModel into the DataModel layer?
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 those "data areas" has add+remove buttons and textboxes to enter data.
Actually each of those "data areas" are put in its own GRID having so the posibility to set a individual datacontext.
1.) Should I now create 3 ViewModels CustomerVM,OrderVM and ProductVM? 2.) Are those 3 "data areas" seen as an own sort of separated View, although I have not put them in 3 UserControls.xaml files ??? 3.) When this one UserControl is inside a TabControl`s tabpage where do I load the 3 entities related data? Inside the MainViewModel? I want to show/load that data only when the user clicks the tabheader.
[HttpPost] public ActionResult Edit(int id, FormCollection fc) { Movie movie = ( from m in _ctx.Movie.Include("MovieActors") where m.MovieID == id select m ).First(); MovieActorViewModel movieActor = new MovieActorViewModel(movie); if (TryUpdateModel(movieActor)) { _ctx.ApplyPropertyChanges(movieActor.Movie.EntityKey.EntitySetName, movieActor.Movie); _ctx.SaveChanges(); } return View(movieActor); }
However, I am not sure how to test this, and in general would much rather have the method take a typed model like:[HttpPost] public ActionResult Edit(MovieActorViewModel movieActor) Is this possible? What changes to my MovieActorViewModel class do I need to make in order to enable this? That class looks like this:
public class MovieActorViewModel { public Movie Movie { get; set; } public Actor Actor { get; set; } public PublisherDealViewModel(Movie movie) { this.Movie = movie; this.Actor = ( from a in this.Movie.Actors where a.ActorID == 1 select a ).First(); } }
The view is typed (inherits ViewPage) simple:
<% using (Html.BeginForm()) {%> Movie Title: <%= Html.TextBoxFor(model=>model.Movie.Title) %><br/> Actor Name: <%= Html.TextBoxFor(model=>model.Actor.Name) %> <% } %>
I have used IDataErrorInfo Validation for my Model.
But when I use these model classes inside a view model, the validation does not happen.
sample viewmodel below
[code]....
Now, if Category or Subcategory classes are directly used as models for view, the validation works fine. But, if CategoryViewModel is used, no validation occurs.
A table can be used to automatically sense Model. But when I get more than one table after the SQL query is a DataTable. When this DataTable to return View (DataTable) later. In the aspx file, can automatically perceive it as the Model, as the self-paragraphs?
But at the same time in this page I need to render some info from another ViewModel. Is it possible to write in action something like return View(Model1, Model2); ?And how could I get access to these ViewModels in this case?
This is probably basic stuff, but I can't find the clear answer anywhere..Lets say I'm doing an MVC (3) application with Entity Framework (EF4), and I have these three classes:
I have a table called "question" which consists of various columns, including 3 columns of type: bit.
these columns are: yes, no, maybe.
The corresponding viewmodel has 3 properties of type boolean. The controller returns the views, which are based on the viewmodels.
By default MVC create checkboxes for boolean types, the thing is that the properties have to be related, only 1 should be checked at a time. That is why I want to change the checkboxes to radiobuttons.
After some search on the web I tried various things, in the end I got the create working by putting three radiobuttons, tied to the same boolean field, adjusing the values.
But how to edit a question? The view is filled with data from the database, putting 1 of the 3 booleans to true.
What should my view be like, to display 3 radiobuttons, mapped to the 3 boolean types?
public class AccessRequestViewModel { public Request Request { get; private set; } public SelectList Buildings { get; private set; } public List<Person> Persons { get; private set; } }
So in my application there must be at least 1 person for an access request. What approach might you use to validate? I don't want this validation to happen in my controller which would be simple to do. Is the only choice a custom validation attribute?
Edit: Currently performing this validation with FluentValidation (nice library!)
RuleFor(vm => vm.Persons) .Must((vm, person) => person.Count > 0) .WithMessage("At least one person is required");
http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspxThat's Great, but it is in #C like every other tutorial I find. Note his ContactViewModelWell here is mine:
[Code]....
How do I add validation in the same fashion as he did in his blog in VB?
I have a little MVC 3 challenge that I am trying to overcome.Here is my scenario:I have a controller called PersonController.cs with 5 methods:Add Add (post) Edit Edit (post) PopulateName(string EmailAddress) - POST
I have a viewdata class called PersonViewData.cs with 3 pieces of data:
Email FirstName LastName
I have 2 PageViews (Edit.aspx and Add.aspx)I have 1 PartialView (Details.ascx) - with 3 textboxes (Name, Email, Phone)I want to reuse this partial view in both the Edit.aspx and Add.aspx Views.
The user should NOT enter the First Name/Last Name - rather, once the user has entered the email address (OnChange of Email textbox), the First Name/ Last Name of person should be populated. This is done using the PopulateName(string EmailAddress) controller method. Meaning, I have an AJAX Form inside the partial view
Then once all 3 fields are populated. The user should be able to submit either the Add or Edit HtmlForm.
Code of Edit.aspx: [Code]....
Code of Add.aspx: [Code]....
The main issue is as follows:I have an AJAX Form inside the partial view. Since now we have a PageView Form and nested PartialView AjaxForm - Form within a Form - the Browser has a hard time with this concept. In fact, when a person enters an email address, the frmEdit/frmAdd gets submitted.
i need to show project information by value of dropdownlist.by using this code i get a list of data to drop down list.now how can i access the first data in ViewData(or record)
Does anyone have any idea why the code below doesn't give me any value but instead gives me "System.Web.Mvc.SelectListItem"? If I don't do a foreach but instead substitute the ViewData with this
<%= Html.DropDownList("PersonOnCallCheckBoxList") %>, I get the correct value. foreach (var person in ViewData["Person"] as IEnumerable) { %> <input type="checkbox" value="<%= person %>" /><%= person %><br /> <% }
This ActionResult is executed multiple time accoording the number user selected from previous page..each and every student id and StudentType is passed by that view to this ActionResult. my question is there any way that in ViewData we can store all these id's and studentType's so that I can use these id' and StudentType's in other ActionResult? bec I need only these two things in other ActionResult? I can implement this using cache but I dont want to do with that.
tempdata variables works like a session variables?
basically i want to do is to when the form loads for the first time the variables should be empty. but the variable should be persisted until i am on that form.
the form contains searching with a submit button as well as the paging . basically my ques is to use what approach should i use?
My doubt is i have two tables with Id as (Primary key) in one table and in the next table i have a Category Id field as Foreign key relationship with the 1st tabl Id field...
I have created a create view for the 1st table.My question is i have created a partial view for the 2nd table,but i need to pass the Id value to the second tables category Id field...So is there any way to do it by using Viewdata?
I am passing certain string value to a view using "ViewData" and I need to print the contents on a window which is rendered using Javascript. My question is how to pass this value to a window which is rendered using Window.Open ?