C# - Difference Between ViewData And ViewModel

Mar 3, 2011

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

View 1 Replies


Similar Messages:

Combine ViewModel (WPF/Silverlight) And Strongly-typed ViewData (ASp.NET MVC)?

Jan 18, 2010

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?

View 1 Replies

Difference Between ViewData And ViewBag?

Jan 16, 2011

I saw the ViewBag in MVC3? How's that different than Viewdata in MVC2?

View 2 Replies

Does 1 ViewModel Really Have 1 View

Feb 16, 2010

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.

View 1 Replies

C# - MVC ViewModel And DropDownList

Jan 30, 2010

I have 2 properties in my ViewModel

class ViewModel1
{
Dictonary<int, string> PossibleValues {get;set;}//key/value
int SelectedKey {get;set}
}

I want to edit this using a Html.DropDownListFor I want to get MVC to auto serialize the data into/from the ViewModel so I can the following

public ActionResult Edit(ViewModel1 model) ...

View 3 Replies

Pass Value Of Id Which Is In ViewData?

Apr 27, 2010

<%#ViewData["id"] %>
<h2>MarkerDetail</h2>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$.ajax({
url:"/Marker/MarkerDetailPartial",
data:"",
success:function(result){
$("#ReplyDetails").html(result);
},
error:function(result){
}
});
});
</script>
<div id="ReplyDetails">
</div>

& i want to send Id only By this Ajax

View 1 Replies

Update A ViewModel In MVC2?

Mar 2, 2010

[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) %>
<% } %>

View 3 Replies

Mvc IDataErrorInfo Validation When Using ViewModel?

Jun 20, 2010

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.

View 1 Replies

C# - MVC - Getting ViewModel Object From ListBoxFor

Jan 9, 2011

I'm trying to get my own viewmodel after submit my form and I got the following error:

"Unable to cast object of type 'System.String[]' to type 'System.String'"

This is my code:

ViewMode:

public class SoftwarePackages
{
public string[] PermissionsList { get; set; }[code].....

View 3 Replies

MVC :: When More Than One Table Associated With - ViewModel How To Write It

Jan 19, 2010

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?

[Code]....

View 4 Replies

MVC :: To Pass More Than One ViewModel To The View?

Sep 5, 2010

In my page there is a Login form. It looks like

[Code]....

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?

View 2 Replies

Need A ViewModel To Take 1-to-many-data To The View?

Feb 23, 2011

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:

public class Foo
{
public int FooID

[code]...

View 1 Replies

MVC :: Bind Radiobuttons With Viewmodel?

Jan 27, 2011

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?

techniques used:
MVC 3 RC 2 with razor
Entity Framework 4 (CTP 5)

View 6 Replies

C# - ViewModel Validation For A List?

Feb 28, 2011

I have the following viewmodel definition

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");

View 4 Replies

MVC :: VB Syntax For Validation In ViewModel?

Jul 14, 2010

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?

View 6 Replies

MVC :: ViewModel With An EntityObject Property BUG?

Nov 5, 2010

I have a ViewModel that contains an EntityObject and when I use a HtmlHelper like this

[Code]....

View 4 Replies

MVC :: PartialView Outside Of Form - ViewData

Mar 16, 2011

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.

View 5 Replies

MVC :: Access To The First Data Of ViewData

May 3, 2010

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)

[Code]....

View 5 Replies

Creating Dropdownlist From ViewData

Jul 22, 2010

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 />
<%
}

View 2 Replies

MVC :: Add Multiple Values To Viewdata

May 27, 2010

I have one ActionResult method

[Code]....

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.

View 2 Replies

MVC ViewData Not Rendering In View

Jul 16, 2010

I have the following code in my post action method for Edit.

JobCardService.Update(viewData.JobCard);
var js = new JavaScriptSerializer();
ViewData["Notifications"] = js.Serialize(new {NoteificationType = "Success", Message = "The installtion was successfully updated"});
return RedirectToAction("Index");

However, on the client, ViewData is null/empty, i.e. this client code

var notifications = eval("<%= ViewData["Notifications"]%>");

renders as

var notifications = eval("");

I'm sure I'm doing something small wrong.

View 1 Replies

Mvc Viewdata Tempdata Variable?

Jul 29, 2010

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?

View 1 Replies

MVC :: How To Set Viewdata As Global To Be Used By All Other Views

Dec 31, 2010

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?

View 3 Replies

MVC :: ViewData Value And JavaScript Function?

Feb 17, 2010

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 ?

View 16 Replies

MVC :: Viewdata Is Defiant Null Always?

Apr 15, 2010

i return data to view by viewdata. and allways checking the viewdata for null but the viewdata is defiant null

this is my code for return data from database

[Code]....

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved