MVC :: Data Annotations, View Models & Multiple Controller Actions?
Feb 17, 2010
quick question: I have a view on my HomeController which is used to create a new 'model'. This view posts to a different controller to keep things tidy. The problem I am having is when I enter invalid details into the form, the rules I specified never seem to make it back to the original view and thus my validation messages or summary never get populated. Is there a way around this because I am using a view model with two IEnumerable lists in it.
View 5 Replies
Similar Messages:
Apr 14, 2010
<%@ Page
Title=""
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<JCIOrange.Models.UserProfile>" %>
How do I display more than one model per view? The above line limits me to only display UserProfiles. I want to also display other models, like UserJob and etc....how would I go about doing so?
View 1 Replies
Jul 4, 2010
I am having two models User and Address. In the create User view apart from the user information, the user can add one or more addresses to himself. So is there a way to do model based validation for multiple models in the same time ?
View 9 Replies
Aug 11, 2010
How do you handle multiple models in a view?. I have 2 tables. Member and MemberAddress
Member class contains Collection of MemberAddresses since a single member may have a billing and shipping address:
public class Member
{
public virtual int MemberId { get; set; }
public virtual string Username { get; set; }
public virtual bool IsActive { get; set; }
public virtual bool IsSame { get; set; }
[Code]....
View 3 Replies
Jan 5, 2010
I'm trying to learn the ropes of ASP.NET MVC and so far so good. One thing which I haven't seen addressed yet is the 'right' or most elegant way to pass multiple data models to a view. To help put the question in context, take this example: Say I was making a blog. When I log in I want the home screen to display a list of all new unapproved comments, as well as a list of recently registered users, and a list of the most recently submitted blog posts.
Most discussions I've seen suggest strongly-typing the view page so it can be called with something like "return View(RecentComments)" and iterate through the comments in the view, or to cast the data model like "var NewUsers = (MembershipUserCollection) ViewData.Model". What I'm ideally after is the 'right', or at least a 'right-enough', way of passing multiple models while still maintaining appropriate logic separation.
View 6 Replies
Dec 12, 2010
I know that view model can be used for rendering a view, but if a page needs different models, how can I pass them to the view? And how do I use them?
View 2 Replies
Oct 26, 2010
So far I've seen demos of MVC that have simple pages, one for login, one for register, one for listing a table. In order to understand better the transition for WebForms developers into MVC patterns, I would like to understand how can I put together multiple CONTROLS (like the one provided by the default MVC project that VS2010 creates for us ...and make it work. OK so as an example, imagine we want to change the LogonUserControl.ascx to allow us to login (so to show UserName and Password textboxes and Logon button) from the Home Page.
1) We update the Control as follow:
2) We update the HomeController.cs file to handle the Logon Button Post event as follow:
We needed to move some code from AccountController to HomeController in order to get the control to work under the Index view. So ...with little luck it seems to work! Now the problem happens when you navigate to the Register view, considering that now we are not any longer on the HomeController "domain" but we are under AccountController. Register page shows perfectly and since the LogOnUserControl.ascx belongs to the MasterPage, it shows too. Since we are not logged yet, it will be displaying the UserName and Password textboxes. But we don't really care about that, because we are on the Register view and after populating the Register values, we click on the Register button. This is the error I get:
>>> To me that I come from the WebForms world, it seems like even if the event fired was the Register button (therefore under the AccountController domain), there is a kind of hidden OnInit event fired for any control on that view, in this case the Logon event. I'm sure this is NOT the case but the real question is... if we are allowed to have only one Controller x view, how can we put together controls that come from different "controller domains" and display them inside a single view?
Could I develop an eCommerce site using MVC that has the home page displaying different controls like [Most Purchased Product, Product of the Month, Special Promotions, Messages Inbox, Company Events ... and each control has it's own button to click and do something (so a proper POST request)] on the home page, unless I only have ONE single Controller which i'm not even sure if it will work or if it would make sense ....since MVC is all about separation of responsibilities.
View 7 Replies
Nov 3, 2010
i have a base controller that looks like this:
[Code]....
and a contact model that inherits from the base model.
the master page inherits form the base model,
nad the contact page inherits from the contact nodel,
but then i get an error while loading the contact page that it expects the contact model but gets the base model?
View 9 Replies
Feb 7, 2011
I'm rendering a gridview from a controller by adding data logic. On the same page I would like to render another grid by binding it to anoither data source / linq query.
When I do it for one, I do the following
[Code]....
How do I render another data source to the same page ?? I havent done it before, and would like to know if it can be done and if so how!
View 6 Replies
Nov 24, 2010
I'd like to fully localize my ASP.NET MVC application, and while there are many articles that deal with the various pitfalls of ASP.NET MVC localization, none of them deals with an aspect that I want to deal with, and that is the localization of URLs, including the controller names and actions.
I would like a way to specify the strings that should appear instead of the controller name and action name in the URL, for example in a resource file.
View 3 Replies
Feb 2, 2010
I am trying to read the response of ResultView in the action of some controller. I need to process the response before returning the ResultView. I am trying the following code:
[Code]....
Whenever I read writer, I get empty string though no exceptions occur.
View 4 Replies
Jun 3, 2010
In this tutorial in listning 2 we have:
[Code]....
What if I need also CustomerRepository and EmployeeRepository ? Should I write:
[Code]....
?? But if I have in ProductRepository, CustomerRepository and EmployeeRepository:
[Code]....
Is it mean 3 connections to database or only 1 connection to database when I open some website controller ProductController ?
View 2 Replies
Nov 4, 2010
I have a parent View that uses a few partial views. Each partial view has its own custom model. The parent View is a model containing each of the partial view's model. In other words this parent View model is NOT flat.
How do i get POST data to bind with the parent's model object? Do i have to write a custom model binder?
Update I'm not binding to a collection of objects of the same type (ie: a collection of uploaded files). I'm binding to object that contains multiple of objects. For example:
[code]....
View 1 Replies
Sep 18, 2010
(Using SQL Server 2008 and ASP.NET MVC2)I have a simple business table and a category table. I have a 3rd table to map business to category, a many-to-many relationship. the Map table contains a BusinessID and CategoryID. I'm lost as to how I can load the categories into my Business View Model using the entities model.Without incorporating categories, my code looks like this:
[Code]....
with Business View Model defined as:
[Code]....
but if i want to populate the Categories with the mapped categories, how would one do that?
View 1 Replies
Jan 27, 2010
Can i create a partial view and a controller that will feed data to the view, and if i render that partial in a Master page, the Data will show on whatever URL i am?
Or is there another way of showing content from database on every page(view)?
View 2 Replies
Dec 15, 2010
I've been used to decorating data model classes with data annotation attributes, but the purist in me baulks slightly at including purely presentational attributes such as display format here. I am, however, quite happy to keep validation centric attributes here. One good reason I have to continue keeping all annotations etc. in the data model is that my view model aggregates data model classes, e.g.
my ViewModelBase.DetailItem<TEntity> property in the view model is just a reference to an entity class in my data model. If I wanted to move presentational annotations to the view model, I would have to quite radically revise my design to one where I duplicate data model properties in my view model and use an object mapping tool to populate view model objects based on data model objects.
[code]....
View 2 Replies
Feb 3, 2010
I have a controller with the following actions...
public ActionResult YellowList()
public ActionResult RedList()
public ActionResult BlueList()
All these actions populate the same viewModel (ListViewModel).
How do I get them all to point to the same view (aspx)?
View 2 Replies
Jun 1, 2010
Like most web applications you have a method that gets called when you log in. There are a few things that may need to get done when logged in and over time this may increase. eg. logging, welcome emails, maintenance. Should events be used to do this or is there a better way?? I'm using C# and ASP.net MVC. Update This is already in its on Service Layer class. eg.
public void Login(User user)
{
SetAuthCookie(user);
LogLogin(user, true);
SendEmails();
}
View 3 Replies
Feb 4, 2011
I am implementing a view for Food. (ASP.NET MVC2) Then depending on the type (say fruit or vegetable for example) I will change the view.
Can I do this without creating a seperate view for fruit and vegetable?
I.e. Say we have url structure like [URL] and don't want [URL] [URL]
So I want to be able to change the view depending on the type. I'm using a tabstrip control from telerik, to give you an of the difference in the views - it'd just be say - not displaying one particular tab for Veg, and displaying it if fruit, for example.
Can a view accept two different view models ? so when we hit [URL] the code determines what type the object is (Fruit or Vegetable) then sends either FruitViewModel or VegetableViewModel ? If I just send one viewmodel how can I control the logic to display or not display certain things in the view?
View 3 Replies
Mar 18, 2010
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:
[code]...
How can I change this?
View 4 Replies
Sep 6, 2010
I'm trying to develop my application and I've got a master page where I'm planning to create a div container, where my "news" will be loaded from my database and will be shown always on a main site. Then inside of the main placeholder I'm planning to load some extra data too from my database... and there's the question: how to solve that ? is there any possibility to pass a model to a master page ? or two viemodels to a view page inside the main placeholder on my page ? (one with news, and other containing some other data).
View 4 Replies
Oct 13, 2010
I have one view page.now what i want to do,i want to bind two models to same view.
I have to bind two different models to single view.
I have bind view with storngly type with Department model First display all the Department, now on click of Department i have to show the Courses associated with that Department.
How can I achieve this.
View 18 Replies
Jan 5, 2011
I have a requirement where i need to use Master Class (a class in my model) as ViewMaster's Model and the View using MasterDetail (another model). i use them in this form and View in this form
<%
@
Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage<HotWorldPOS.Models.MasterNavigationModel>"
MasterPageFile="~/Views/Shared/Site.Master"
%>
[Code]....
I get an exception when i run this
The model item passed into the dictionary is of type 'Models.GeneralMasterModel', but this dictionary requires a model item of type 'Models.MasterNavigationModel'.
my requirement is like this, the viewmaster is of three column layout, first column showing an IEnumerable containing some navigation links, the third column contains an IEnumerable of MasterDetail class depending on the Master object clicked, and the center column is the content place holder for displaying child views (for editing, displaying selected masterdetail). this view's model will be deferent for each masterdetail selected.
View 2 Replies
Mar 8, 2011
Is it possible to do something like this?
Inherits="System.Web.Mvc.ViewPage<List<SectionAdmin.Models.ADirectory>>, <List<SectionAdmin.Models.BDirectory>>"
I am trying to use both models in my page.
View 8 Replies
Dec 21, 2010
I want to develop a user control (by inheriting UserControl) which contains a button which when clicked does multiple actions. Those actions should be defined somewhere. So when I sue the control on page1.aspx the button does action X, when its used on page2.aspx and the button is clicked there, action Y is started and so on.
View 4 Replies