MVC Preview - Part 2 - Editing Data and Making Forms

This second screencast on the Mix release of ASP.NET MVC, Scott Hanselman builds on what we learned in the first video and creates forms and discusses the new Html Helpers. We’ll also introduce ViewUserControls to avoid repeating HTML in two places.

Be sure to watch the other three videos on ASP.NET MVC

ASP.NET MVC is available in the ASP.NET 3.5 Extensions Preview (December 2007) and the ASP.NET MVC Preview 2.

Presented by Scott Hanselman

Duration: 18 minutes, 38 seconds

Date: 28 February 2008

Watch the video   |   Download the video

Video downloads: WMV | Zune | iPod | PSP | MPEG-4 | 3GP

Audio downloads: AAC | WMA | MPEG-4 | MPEG-3 | MPEG-2

Comments : 13

Leave a Comment

random0xff : On March 06, 2008 3:26 PM said:

So how to get error messages for required field etc next to the form fields, or change the form field's style based on an error. Is that kind of binding possible as well?

simonech : On March 10, 2008 3:45 AM said:

Hi Scott,

great screencasts... maybe, it would be nice having a link to download the samples shown in the videos as well.

And also, a kind of bibliography, with the url of the posts referenced.

Thanks for the great job

Simo

coppermill : On March 11, 2008 8:47 AM said:

Little bit concerned over using <% %> within asp.net pages, as this reminds me of classic asp, and the mess you end up with

braddunbar : On March 14, 2008 2:10 PM said:

Scott,

About halfway through the video, during your explanation of strongly typed ActionLinks, you state that "a broken build is always better than a production break," which makes the strongly typed version of this method superior.  This implies that changing a route/controller without changing your call to ActionLink<Controller>(c => c.Action(), "Link") would break your build.  Since the view page is compiled at runtime and not during a build, how does this break your build?

Are you referring to dynamic page builds at run time or am I missing a setting in Visual Studio?

Martin Lercher : On March 14, 2008 4:14 PM said:

Hi Scott,

I'd be glad to see a sample of how to implement editing using aggregated controls for structured data. Say you want to edit an instance of:

class invoice (view invoice.asx)

 class customer (with a field "customer" and view customer.ascx)

   class naming (naming.ascx, a view<naming>)

     string first

     string middle

     string last

   class address (address.ascx, a view<address>)

     string street

     enum country

 class item (item.ascx)

   double price

   string description

...

each class has it's own view .ascx control and now you want to assemble a specific view (mashup) of invoice as viewdata based on a url like http:.../edit/customer.naming,customer.item, whitch takes the view controls for naming and item, binds them to viewdata.customer.naming and viewdata.customer.item respectively and put's it back to the model data.

shanselman : On March 14, 2008 5:49 PM said:

random0xff - Good idea, I'll start looking into the best way to do Validation.

Simone - Yes. I have minimal control over these pages but the code is all on my blog at http://www.hanselman.com under the ASP.NET MVC category. In the future, however, I'll plan to get the samples up in a more formal place and link to them from the videos.

BradDunbar - Changing the route wouldn't break the build, but changing the Controller action method WOULD as the lambda wouldn't compile any more.

Martin - Great idea. I'll add it to the (growing) queue.

ovesens.NET : On March 16, 2008 5:51 AM said:

Hi Scott

Thanks for a great video. I have however some comments and thoughts.

First, I have to get used to the fact that you have removed the [ControllerAction] attribute. I liked it better in preview 1, but then again a public method is public.

I like the way you used the Binding.UpdateFrom. However from a security perspective, there could be problems. I have worked on systems where a class (e.g. Order) have two different views where it can be edited.

The first, where the customer for instance views all the order information, and are able to change the reference, the delivery address etc. but not the price.

The same order could also be viewed and edited by the company, and they should be able to update the same information including the price.

If an evil customer knows how the application is developed, they could inject:

<input type="text" name="Order.Price" value="[very small price]" />

And hit update, and then update the price even though they shouldn’t be able to.

Do you have any thoughts on how to deal with this, or should we just not use the Binding.UpdateFrom and do the update manually?

I hope my problem is well described, if not then please feel free to contact me.

Thanks

Mikkel

jfiorini : On March 18, 2008 9:30 AM said:

Scott,

Awesome demo, thanks!  You are probably already planning on this, but I would really like to see Html.TextBox automatically infer the value if you are using the convention ModelName.PropertyName ala Ruby on Rails & Monorail.  I have written some code to accomplish this and posted it on my blog at www.faithfulgeek.org/.../making-htmlhelper-textbox-act-like-it-should.  Let me know what you think; I'd love to hear if you are planning this!

-Joe

shaunmccarthy : On March 20, 2008 3:07 PM said:

Hey Scott

I am really enjoying playing around with MVC and SilverLight 2.0 - great work :)

Having embraced the xaml syntax in SilverLight, I am a little concerned with the <%= %> syntax / Html helping class in MVC. Sure, it's great for Rapid Application Development, but the minute you want to do anything to the text box produced (html classes, sizes, javascript events etc), the syntax gets pretty awkward pretty fast. I am glad you didn't go down the route of 5000 constructors for every possible attribute combinations, but even the htmlAttributes syntax gets clumsy fast, e.g:

<%= Html.TextBox("SomeValue", ViewData.SomeValue, new { Class="html", onblur="dosomejs();" }) %>

You lose type saftey, you have to worry about reserved words (like class) etc. This could be addressed with objects for each Html helper tag, e.g.

<%= new Html.TextBox{ Name="SomeValue", Value=ViewData.SomeValue, ClassName="html", OnBlur="dosomejs();" extendedAttributes= new {someWeirdHtmlAttribute="value"} } %>

It still seems a little clumsy compared to asp: tags for static attributes - though it's much clearer when dealing with dynamic attributes set via code. A hybrid would be great, but that would probably have to wait till VS 201X.

My other concern is that the html design surface is actually almost _usable_ now in Visual Studio 2008 - and it's especially helpful with it's style editor. However, since MVC uses the <%= %> syntax, you lose all that goodness...

Hope this feedback helps & thanks again for the tutorials,

Shaun McCarthy

http://neofight.wordpress.com

alivemedia : On April 02, 2008 10:42 PM said:

How do I do this in VB.net:

<%=Html.Form<ProductController>(p=>p.Update(ViewData.Produc t.ProductID))%>

I searched everywhere for the correct syntax

ChrisLomax : On April 10, 2008 5:06 AM said:

I think the code you require is

<%=Html.Form(Of ProductController) (Function(p) p.update(ViewData.Product.ProductID)) %>

Its hard getting information for us sole survivors on the vb.net language. Everything new coming out is written in c#!

What you really need to read up on is Lambda expressions for vb.net

Dont quote me on the above function call, I have not tested it and i am new to all this as well, i did implement a version of this but since used another method so i took the code out else i could have copy pasted that as that did work.

BTW, love all this MVC stuff, i have been out of .NET for about 2 years now as i moved on with PHP, a project has come on recently that requires me to learn .net again and this has been a great addition that i have really enjoyed easing myself back in with.

Cheers Scott!

Is there still no word on a final release date for this?

rassidm : On May 08, 2008 3:57 PM said:

Scott,

Awesome demo, thanks!

I can't find the code of the samples shown in the videos on http://www.hanselman.com -> ASP.NET MVC category.

it would be appreciate if we can have the direct link to download the project of the sample.

Thanks for the great job.

rassidm : On May 08, 2008 4:58 PM said:

finally i find the code sample in the ASP.NET MVC category :)

Thanks again and again

Leave a Comment

You must be logged in to leave a comment. Click here to log in.

Sponsors