Showing posts with label mvc. Show all posts
Showing posts with label mvc. Show all posts

Wednesday, February 10, 2010

Not Validating A Complete Model

I am currently trying to edit a single model object through a series of views, using the MVC 2 RC 2 framework, found here:
I discovered that while using the default server-side validation that my model could not be properly validated because some of the model properties were not fully populated, or had incorrect values when, for example, the first view's form was submitted. The incorrect/null/etc values were fine for me because the process had not reached the point they would be populated, but the ModelState.IsValid still returned false, of course. This would be fine, except I needed it to be true, fitting to my standards of what was 'true' about the state of 'my model'.

The method I came up with follows, but I will say that it's not perfect and will almost certainly be superseded by something better in the future. Here it is:

///
/// Used simply to ensure that validation has occured for the model properties that were rendered on
/// the current view.
///
/// The list of form field names to check for errors. Eg: the FormCollection object
/// True if none of the ModelState values matching the whitelist keys have errors attached, else false.
private bool IsModelStateFieldsValidated(IEnumerable whiteList)
{
foreach (string key in whiteList)
if (ModelState[key].Errors.Count > 0)
return false;

return true;
}

If the action method takes a 'FormCollection collection' parameter then that can simply be passed into the IsModelStateFieldsValidated method directly.

The method above just very simply checks the field names which were on the page (which, of course, may not be every property in the model class) and checks that there were no exceptions found in the ModelState value collection - for those property names only. Any other exceptions will be ignored, because they are not pertinent to the current view.

Another Problem Solved By Someone Better Than Me

Ok, this is one of those woods-for-the-trees situations. Basically, I had been mucking about with custom model binders:
And had added a class attribute to my model class to handle the post back binding of the model. This worked fine. The following, however, I forgot about this and was trying to use the default server-side validation.

Problem: The server-side validation was not working.

Solution: Remove the custom model binder.

Reason: It was returning just an empty model object every time.

This solved a host of problems, in fact.

The EditorTemplates Directory

Having been trying to get a model property to render the appropriate editor in MVC, recently, I was having some trouble getting the editor - provided by a very talented colleague - to render correctly, or, in fact, at all.

Not having been introduced to the structure of the shared directory (it doesn't seem to be mentioned in the http://www.asp.net/learn/mvc/ pages) there was an issue with understanding how the Html.EditorFor method would provide my custom editor.

This was solved when I came across David Hayden's blog page:

Monday, February 08, 2010

MVC's HtmlAttributes Parameter

In MVC's model it is very easy to generate HTML tags, or XHTML elements, as I believe they prefer to be known these days.

What wasn't so obvious was the optional htmlAttributes parameter, available in all the Html.TextBoxFor(...) Html.LabelFor(...) etc etc.

My question was, "How do I use this, what's it for, why, when, where, who, whether, whence, etc?"

Yeah, I'm not so great at forming questions sometimes. But the answer came in the form of Rob Conery's blog entry:
And so I've learnt that you can simply have disabled="disabled" in a new {} and the ...For adds the values as attributes to your HTML element. Read Rob's article for a better description.

However, if you're still having trouble with this (eg: ActionLink doesn't seem to link certain htmlAttributes assignments) you can check out this post at stackoverflow:
Top notch.

Yes, I need to get the code embedding sorted on this 'ere blog thingy...

Friday, February 05, 2010

Finding Out More About MVC

[EDIT] Ok, irony of ironies... Literally AS I was writing this, the MVC team at MS were putting out their latest version: MVC 2 RC 2. You can get it here (and yes, it fixes the problem I've described below): http://weblogs.asp.net/scottgu/archive/2010/02/05/asp-net-mvc-2-release-candidate-2-now-available.aspx


So, after some working with MVC 2 RC (Full title: ASP.NET Model-View-Controller Version 2, release candidate, or so I'm told ;) I discovered some lovely things about MVC. One of which, in particular, is the TryUpdateModel, or UpdateModel, method available in the Controller class. This is supposed to take a FormCollection from your Edit (et al) Action method (ie: response from a postback) and populate your model instance for you. It should do this by using an optional whitelist and blacklist of class property names and the names of the fields on your View's form, then by using reflection to assign those properties values.

You'll notice, of course, that the above tends towards the opportunity rather than the specific. This is because the only output from the TryUpdateModel I've had is an error stating "Value cannot be null or empty. Parameter name 'name'." Woo hoo.

Anyway, fortunately there's lots of help on this subject, below... and hopefully my own solution, later...
And so, in the end, I've had my first look at downloading the source for an open MS framework and there are lots of things to be learnt in reading other organisation's code. It's an interesting project and doesn't have to be long and painful. Just grabbing a download and browsing should throw up an idea or two and hopefully show the right or wrong way of approaching things. Or, at the very least, a different way.

Also, I just wanted to point out that these are a great pair of posts:

Friday, January 29, 2010

Setting Up MVC

Having moved to the RC version of ASP.NET MVC 2.0, I've been looking around for resources (some repetition follows):

Friday, April 17, 2009

Small Post About MVC

Looks like there's some movement in the ASP.NET MVC market, finally, so lets get some links up:
Some MVC resources:

Saturday, October 04, 2008

Getting MVC setup

This is all about getting MVC Preview 5 running: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16775

ASP.NET Project page: http://www.asp.net/mvc/

Had a bit of trouble getting MVC setup. A nasty little bugger going by the name of "Cannot load assembly ..." etc. You know the score.

One post on forums.asp.net sorted it all out:

http://forums.asp.net/p/1314519/2596828.aspx

I short, do the following:

Go to: C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft\Microsoft Visual Studio 2008\Visual Studio Tools

Run: Visual Studio 2008 Command Prompt

Go to: C:\Program Files\Microsoft ASP.NET\ASP.NET MVC CodePlex Preview 5\Assemblies

Type:
C:\Program Files\Microsoft ASP.NET\ASP.NET MVC CodePlex Preview 5\Assemblies>gacutil -if System.Web.Routing.dll

And:
C:\Program Files\Microsoft ASP.NET\ASP.NET MVC CodePlex Preview 5\Assemblies>gacutil -if System.Web.Abstractions.dll

That should install the DLL's into the GAC (Global Assembly Cache) and cause .NET 3.5 to find them suitably.