asp.net-mvc-2

How to redirect from Application_Error in ASP.NET MVC 2?

I want to show a custom error page if a user tries to upload a file larger than the maximum request length. With no code at all, I get a very mysterious "The page cannot be displayed" error when uploading a large file (not the famous yellow ASP error page) -- the same sort of browser error you get when you're offline. This strikes me as...

How to set a Foreign Key relationship manually in LINQ To SQL

I've been working through the book Pro ASP.NET MVC 2 Framework by Steven Sanderson. So far it's been phenominal... just when i think I know a decent amount I find a book that shows me just how little I know. One of the things I know little about is how to use LINQtoSQL. In Steven's book, chapters 4-6 create a very nice little shopping c...

Why is my MVC ViewModel member overridden by my ActionResult parameter?

Is this a bug or a feature? All code below has been simplified for the sake of brevity and easy replication and does not actually do anything useful other than highlight the behavior. I have a class that includes an int named ID: public class FooterLink { public int ID { get; set; } } In my controller, I have an Edit actionresul...

Why should I use GetOriginalEntityState() in my LINQ To SQL repository save method?

I'm looking at an example of a save method in a Products repository from Steven Sanderson's book, Pro ASP.NET MVC 2 Framework: public void SaveProduct(Product product) { // if new product, attach to DataContext: if (product.ProductID == 0) productsTable.InsertOnSubmit(product); else if (productsTable.GetOriginalEntit...

Validating a model in ASP.Net MVC2.

I have the following database table: And here is the code I use to validate the model created by Entity Framework: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace UTEPSA.Models { [MetadataType(typeof(Area_Validation)...

Asp.net Mvc2 Data Annotations Validation (Client Side works, Server side Doesnt??)

I'm using an entity model with metadata annotations. My controller method looks like this... if (!ModelState.IsValid) { return View(model); } else { UpdateModel(model); repo.Save(); return RedirectToAction("Index"); } If I enable client si...

What is the fastest way to convert a class to XML

I would like to know what is the fastest and most lightweight technique to convert a fairly large class to XML. The class will have lists and arrays in it. I need to convert all this data to XML Here is what my application does: it will get all the information from the database using linq to enties. Then store the data in a class. Then ...

Mocking a session wrapper in MVC2

I've seen how to Fake the SessionState object in MVC using Scott Hanselmans MvcMockHelpers, but I'm dealing with a separate problem. What I like to do is create a wrapper around the Session object to make objects a little more accessible and strongly typed rather than using keys all over. Here is basically what it does: public class S...

Html.ActionLink does not display anything

I have this simple snippet with an ActionLink that is supposed to display some text as a link, but it's not working. Here's the code snippet. <div id = "Div1"> <table id = "Table1"> <% while ((category = SomeNamespace.Helper.GetNextCategory(categoryIndex++)) != null) { %> <tr> ...

How to use a namespace within an MVC view?

This should be simple and I don't know why the compiler is complaining. I have a view and I want to shorten this call inside it: <div id = "catalog"> <table id = "catalogContainer"> <% while ((category = SomeNamespace.Helper. GetNextCategory(categoryIndex++)) != null) to not quali...

CSS not working

Sometimes the dang thing works and at other times it doesn't. I have many tables in my app and the CSS for all of them is working. There's nothing different for this one, except for it, the CSS isn't being applied, God knows why. Help. table.catalogContainer { border: none; padding: 50px; margin-left: 100px; margin-right...

Issue with generating a view with multiple rows in ASP.NET MVC 2

I am running into an issue with creating a view which has multiple rows for either creating or editing. The scenario is such: I need to create a view to enter the results of students. The functional logic required is that the user would select a student from a list and depending on the student's grade, the relevant subjects need to pop...

ASP.NET MVC SiteMap with Ninject controller factory error

I am trying to use the ASP.NET MVC Codeplex Sitemap project with Custom Dynamic Node provider. This is my node provider code. It uses some repository to access the DB. I actually don't use DI in this class but the application inherits NinjectHttpApplication and the Sitemap has problems acquiring controller. public class ContentPageDynam...

Validation Attribute - checking a dynamic number of fields on a form ASP.NET MVC 2

Hi there! I have a number of forms with checkboxes that need validating. For example I might have a form with 3 checkboxes and one of those 3 needs ticking and another form with 50 boxes that might need at least one item checking. Does anyone have a ValidationAttribute that would work under these circumstances. Many thanks for any poi...

Getting the Action during model binding

Hi, Is there a way of getting the Action, and reading any attributes, during the model binding phase? The scenario is this: I've got a default model binder set-up for a certain data-type, but depending on how it's being used (which is controlled via an attribute on the action) I need to ignore a set of data. I can use the RouteData o...

ASp.NET MVC 2 - find a view embeded in the compiled assembly

Hey There I have an MVC Application the views was embedded in the compiled dll, how can i get a list of all the views? I am using reflection to get all the controller info, however, i do not know the view info. ...

Pointers for writing a unit test for a Razor based View needed.

I'm trying out the new Razor view engine with MVC 3 Preview 1 and would really like to write a simple unit test using NUnit/Moq. I haven't seen any examples of this actually being done yet - despite it being one of the real selling features on Razor. So, if I have a Controller, that uses a DBConext object (EF4 CTP code first) and the vi...

Javascript code in asp.net master page dependent on currenty open page.

Hi, I have a problem with my JS script. I have written a function in my Site.Master because I use it in every page of my website. Right now I need to add a new line to that function (execute another function) but, and this is the whole problem, the function to be executed is only defined in one of my content pages. To be honest it does...

MVC submit form to different controllers

Is it possible to post the same form to different controllers? Each page could be post only to form action url, but may be some how i may say to button to which url form should go? e.g i have one form and two submit buttons, one button will post form to one controller/url (eg /action/view) anther button submit form to one to another co...

System.MissingMethodException: No parameterless constructor defined for this object.

I'm using MVC 2.0 with a Html.ListBoxFor as below: <% using (Html.BeginForm()) { %> <input type="submit" value=">" /> <%= Html.ListBoxFor(x => x.lstTest, new MultiSelectList(new [] {"someone", "crap", "why"})) %> <% } %> When I click the input submit button below with nothing selected, it posts back fine, when I ...