Curious what the best practice is for returning errors to a view from a controller where the error isn't really a validation error, but more like "user not found" or "service timeout" type of errors. (in ASP.NET MVC2 framework)
I've been adding them to the ModelState's model errors, but that doesn't seem appropriate. (although easy to implement and maintain)
Example - A user tries to log in, and their credentials do not match a known user.
I've got a view model that has some text properties. And I use Html.DisplayFor to display them on the screen. But those text data won't be post back, so in the case of failed validation, the returned view won't have those data. How do I handle this kind of situation?
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.
Say I got a domain model as follows: (and my repository expect an instance of this object)
And a view model (which my views are based on)
At the moment I got it like this and have my controller action manually create a new Person object from the PersonModel object before passing it on to the repository, which does not feel right.
So I tried to have PersonModel inherit from Person and pass that to the repository (also tried casting the PersonModel to a Person object first), but that don't work out.
What's the right way to have PersonModel automatically cast to Person? I want to keep this logic as my current structure allow me to keep things really loosely coupled, with the repository layer not having a clue how it's being used.
As i start to work with MVC simple things are not so simple. I have a view that renders correctly a single table of my Database (using Entitry framework created automatically by visual from my SQL database).The table show everything including the foreign key and i i seeking for replacing the foreign key by a friendly name of my foreign table.In the controleur i have the following code :
public ActionResult Index()"Equipement"] = new SelectList(_entities.T_E_EQUIPEMENTS_RESEAU_EQP.ToList(), "EQP_ID", "EQP_NOM"); return View(_entities.T_E_CONSOMMATEURS_CSO.ToList());
both table are joined by the foreign key CSO_EQP_ID and the primary key EQP_IDI have no clue of what to define in the view ?Any exemple of this simple need will be welcome.
I'm currently using a GridView and I want to set the CssClass for the Row depending on a property of the object that the row is being bound to.I tried the following:
<asp:GridView id="searchResultsGrid" runat="server" AllowPaging="true" PageSize="20" AutoGenerateColumns="false"> <!-- The following line doesn't work because apparently "Code blocks aren't allowed in this context --> <RowStyle CssClass="<%#IIF(DataBinder.Eval(Container.DataItem,"NeedsAttention","red","") %> <Columns> <!--............--> </Columns> </asp:GridView>
Now I could simply handle theGridView's RowDataBound event and change the css class of the row there...but I'm trying to keep a clear separation between the UI and the page/business logic layers.
When are declared values bound to properties of a user control in WebForms?
I have a user control which has a public property which is an enum type. In my aspx page I'm setting it's value declaratively. In the ascx I'm outputting the value to the page using <%= %> syntax. The value that is output by the echo syntax is always zero 0. The enum does nopt have a zero value.
My question therefore is, is echo <%= %> syntax evaluated before the declared value is bound to the property?
Then when the user clicks an Edit button I run a script that enables all the checkboxes (which works fine), and then when the user then clicks on a checkbox the tick is appearing or disappearing as it should.
The problem I'm having is that when I try to read the value of the checkbox from the codebehind:
I have an entity object as model input in my view. Not sure if it's the best approach, but I use the object references to get values from a related object.Let's say I've got a car entity and a manufacturer entity.Here's how it would look in my view
I've get a nullreferenceexception where the car entitity does not have a reference towards manufacturer. I'd like to output a " " if no manufacturer exists.
Am trying to get a working enity model with foreign keys working but have run into a problem. I have imported 3 tables with content and some data.Lets call these tables geo_countries, geo_counties, geo_municipalities and the FK are:1. geo_county have a column country_id wich corresponds to country_id in geo_country 2. geo_municipality have a column county_id wich correspond to the county_id in geo_county.The problem i have is when i try to get correspondig child items with the following code i run into trouble:
The problem above is that 'test' generates results but not 'test2', wich never return anything. Am i not supposed to be able to do it like i do in 'test2'? I earlier tried to add a child object by replacing ToList() with Add() and that worked. But i cant seem to get anything out of it.
Im using LINQ2SQL and i have an object called Article. Its has 2 properties FinishText and SwedishText. The idea is to let the user choose language. Swedish is default.
I bind the objects to a listView and there is a label that takes the swedish language. When the user presses the Finish flag button i want the objects to reload and the finish text to show instead of the swedish. The problem is that the text label is bound to the SwedishText property in the mark up like this:
I can think of some ways to solve this, and i have one that doesnt work that well. My question is, what would be a good way to solve this? Im not so experienced so i know that there are lots of you out there that know how to do this much better.
Another problem i dont like my solution to is when i have an object that has a association with my Article object, like ArticleCategory. The ArticleCategoryId of my Article is, lets say 31, which is corresponds to the category "Movies". I dont want to display the category id but rather the name of the category itself (Movies).
So this is what i do: <%#GetCategoryNameFromId(DataBinder.Eval(Container.DataItem, "Article_Id"))%>
I call a method that recreates the artice object and from there creates the ArticleCategory and gets the name from it. Its a horrible solution cause it involves lots of trips to the Database. Especially since i do similar things with other properties.
Just wanted to hear what is a proper and good way to deal with these common but for me new tasks.
've created a Search partial view and it works, except for my view model. What i mean is, the partial view sends a string to my ActionResult Method, but only a string, that is not related to the view model that the partial view is built in. If i tell my ActionResult method to receive the view model in which the partial view is built in, it always tells me that i have null values. Here's my code This is my partial view:
Now here is my Model in my MoviesSearch view model:
one thing that has been puzzling me since learning MVC2 is the following case scenario: I have a view which contains two latest news lists, a login form and a signup form. Every example I found on Views and View Models so far has a one-to-one example such as a simple login form etc. But how do I create a model that provides the properties and validation for a login and signup form and manages the data for the news lists. can I pass multiple models in the strongly typed view? When I created one model the form validation would fail as it expects all fields - login and signup to be filled. I am missing some advanced examples or information.
making my first steps with asp.net mvc and I actually create a (very) small website.I have 1 controller : TasksController1 view : Tasks/Index.aspx1 Partial View : Tasks/AvailableSorting.ascx In my controller I have 2 methods :
In my Tasks/Index.aspx, I add my partial view (<% Html.RenderPartial("AvaliableSorting"); %>)
That sounds simple. But, It just doesn't work : the Index return a list of Task and the parial view (should) return a list of string.The index.aspx works perfectly, but the partial view doesn't get the right model ! he gets the model of the Index page (list of Task).
I am totally a new bee in asp.net mvc. Let me now explain my issue. I have a strongly typed view which inherits from a view model Document. I want to have a partial view in that view itself. My code for this purpose is :
I have a database with a 1 to many relationship in SQL Server 2008. I have created a formview in VS2008 to insert records into the child table. However I can't set the foreign key to automatically insert this value. If I manually type in a value the insert works but I need it to pick up the fk ID automatically from the master table. How can I do this?
The master table: tblTopic has ID int auto increment primary key and the child table has ID int auto increment primary key and TopicID int allow duplicates no nulls, foreign key. I have set updates to cascade in the relationship.
My insert statement is INSERT INTO tblTopic (TopicID, Comment, SubmittedBy) VALUES (@TopicID, @Comment,@SubmittedBy). The TopicID keeps throwing the error, this is the foreign key.
This is the first time I have tried to do this, so apologies if it's really obvious but I'm on a bit of learning curve.