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
Similar Messages:
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
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
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
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
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
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
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
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
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
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
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
Nov 5, 2010
I have a ViewModel that contains an EntityObject and when I use a HtmlHelper like this
[Code]....
View 4 Replies
Dec 15, 2010
I'm curious as to what people consider better practice, between duplicating model structure in the view model and using a mapping tool to move data between the two, or aggregate the model inside the view model, i.e. have a property on the view model class that is a reference to the actual model. Which is considered a better approach in general?
View 1 Replies
Jan 7, 2011
I'm making an MVC2 app to manage billing schemes. These billing schemes can be of various types, e.g. daily, weekly, monthly, etc. and all types have their specific properties. My class structure looks like this:
public abstract class BillingScheme { /* basic billing scheme properties */ }
public class BillingSchemeMonthly : BillingScheme
{
public string SpecificMonths;
}
//etc. for other scheme types
I retrieve a billing scheme through the base class using the billing scheme ID, and it gives me an object of the correct type. The property SpecificMonths maps to a database varchar field that contains a ;-separated string of month numbers. I want to split this to an array so I can create a list of checkboxes and I'd preferably do this with a facade property in a viewmodel. I could do it right inside the view but that seems to go against the MVC pattern.
The problem is I can only create this viewmodel specifically for a BillingSchemeMonthly, and I can't cast a BillingSchemeMonthly to a BillingSchemeMonthlyViewModel. I'd rather not implement a clone method in the viewmodel since the entire billing scheme is quite large and the number of properties may grow.
View 2 Replies
Sep 3, 2010
I am just getting started with MVC after many years of WebForms development.
I have a very simple page with two textbox fields, one called Input where a user will enter something, and one called Output where I want to return some string after they post the form, much like a postback scenario in WebForms. I have the View connected to a ViewModel for strong typing.
The ViewModel:
[Code]....
The View:
[Code]....
The Controller:
[Code]....
The problem I have is that the output text "Here is some response" never gets displayed, even though I modify the value in the HttpPost method and return the viewmodel to the view. Unless (just tried it) I set the textarea to disabled with:
[Code]....
Can someone explain this behavior? It seems as some "magic" assumes that whatever was in the input fields before the HttpPost should also be there after the post, similar to what ViewState does in WebForms.
View 2 Replies
Nov 8, 2010
I can bind to properties in the ViewModel fairly simply like so:
<%=Html.RadioButtonFor(m => m.isCool, true%>cool<br/>
<%=Html.RadioButtonFor(m => m.isCool, false)%>not cool<br/>
but what if I wanted to bind to collection items within the ViewModel. I'm not sure if it's possible. Here's what I have:
My ViewModel:
[code]....
I'm getting the following error: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
View 1 Replies
May 7, 2010
Is there a way to stuff my ViewModel into an Ajax.ActionLink? edit I'd like to take my 5 search fields on my page which are bind to a view model and send it along my .ActionLink as my object value parameter.
View 2 Replies
Jan 11, 2011
I have a viewmodel with a property of a model class type. I set some properties in the model class to be able to show it to the user but then when I make a post, the model gets validated. How to overcome this? I don't want to enforce the DataAnnotations contraint in this case....
public class TheViewModel
{
TheModel TheModel { get; set;}
}
[code]...
View 1 Replies
Sep 15, 2010
I need to create a dynamic form using the already existing DB entries, from 3 different tables. I am trying to do this by inheriting ViewModel class, which is comprised of all the model instances (or List of instances of unknown count) within it.
When I inherit the ViewModel in the View Template, the dynamic form gets generated without any issues, and looks fine. The dynamic form is generated based on iterating over the calls for each required model instance in the ViewModel passed to the View.Each call uses a partial view of the instance to generate the form, and hence multiple iterations create multiple instances of the form.
[Code]....
However, when I try to submit the form, the postback does not include the ViewModel object, but rather only the first model instance used in the form.
View 2 Replies
Dec 9, 2010
I use viewModels to communicate between my controller and my view. To get model validation, i use a partial class like this :
[MetadataType(typeof(EvaluationValidation))]
public partial class Evaluation
{
public class EvaluationValidation
{
[DisplayName("Title of evaluation")]
[Code]....
View 1 Replies
Apr 12, 2010
I am trying to create a view that would contain a set of fields that tie to an AgencyAddress object, an add button, and under those fields, a list of AgencyAddress that would show addresses that were added. My view works, and the fields are properly bound, however I can't seem to get the list to maintain previously added addresses.
What happens, is that every time the Add button is pressed, the List is re-created (or VS yells at me to create a new instance) and only contains one record. I'm sure I'm missing something simple, but I can't figure out how to pass the list back and forth and be able to add to it. Two weeks on my own is where I draw the line! My ViewModel:
[Code]....
My Create controller action (HTTP Get) is here:
[Code]....
And finally, my HTTP Post Create action:
[Code]....
The loop in the view that should show addresses:
[Code]....
View 3 Replies
Jul 1, 2010
I am currently designing a new .NET application and would like to keep it UI independant.
Whilst I initally would like to use WPF, I would like to have the option of swapping the UI to ASP or WinForms if necessary.
In a layered design:
View - Interface Controller (ViewModel) - Model - Persistance
is it possible to design the Interface Controller so that it will work with different view technologies, or will I need to replace the interface controller at the same time as the View?
View 2 Replies
Mar 23, 2011
I have the following scenario;
<%
using (Html.BeginForm("actionname",
"ControllerName", new
[code]...
View 1 Replies
Jun 17, 2010
i am creating inintalizing a viewmodel with the following code in an create action;
[Code]....
the class applications is a LINQ to SQL Class and it has a property called VacancyID.
I want to default this VacancyID to '3'. From within this Action..
View 2 Replies