Thanks for reporting this - there indeed is a rendering bug that I just found (thanks to you!) where the passed in attributes aren't being written. I'll work to get a refreshed zip up as soon as I can - I'd like to wait and see if there are more bug reports.
Marked as answer by CVertex on Dec 10, 2007 09:45 PM
There's like a billion overloads I can understand how'd you'd miss one case.
While I've got your attention, I was hoping you could answer these questions:
1. Why do I have to prefix C# keywords with underscore for helper attributes (like new {_class="blah"}? Why not @ like senor C# allows?
2. I like the linq expression first parameter for typed ActionLinks/Forms, but I found the requirement to include a dummy parameter a bit confusing. e.g. using expression ac => ac.Create("name","blah","blah") on a Form<T>() will still correctly change the
3 arguments to the post fields. I found this cool but a bit confusing. It's like I have literals in my code that are just dummy place holders. Maybe I'm using the linq expression wrong.
3. I'd like to see a typed version of common attributes for HTML elements instead of always accepting object attributes. I know it'll clutter the code a bit, but it feels a bit weird passing anonymous types around. What effect does this have on performance?
Having to use reflection all the time to get attributes? Is performance a concern, or is reflection quick enough? I was always encouraged to avoid reflection in rendering situations for performance reasons. I guess the tradeoff is worth it.
4. I wrote a simple event scheduling system for my friend as an exercise, but I got stuck on paging. Do you know if ScottGu has that PagedList<T> lying around? When he's not looking, can you jump on his box and nab it for me? That's if asking him nicely
doesn't work.
1) Cause I didn't know any better :) - you can do both if you like
2) I'd say you only want to use this type of Form constructor if those values are needed for the action. Tell me more about what you're trying to do - I think there's some confusion.
3) Reflection can indeed be "spendy" - but it depends what you're trying to do. In this case we just grab property info from Type() and that doesn't cost that much. In terms of "typed' arguments - it would really clutter up the signatures (there are tons
of attributes we'd have to account for). We can, however, overload this to take some kind of hash/List<>, but then you're cluttering up your UI.
In terms of performance, we do use reflection all over the place. We haven't yet performed our usual suite of stress and perf tests yet. That is definitely going to be tested before we ever release a 1.0.
If we do find hot spots that are slow, we will probably use LCG (Lightweight Code Generation) to speed things up.
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
Marked as answer by CVertex on Dec 15, 2007 09:29 AM
MS MVC could add more generic expression so you could do something like this:
public RemindersController : Controller
{
public void Search(string query, int page)
{
}
}
Html.Form<RemindersController,string, int>( (c, q, p) => c.Search(q, p)))
Its a little more terse but handles your dummy scenario by not using anything. I really haven't looked at at Html.Form enough to even know if this would work but I do know it works in Linq.
Look at DynamicMethod. Its like reflection emit only faster without the overhead of creating a separate assembly. There are plenty of examples using it if you google around.
CVertex
Member
147 Points
128 Posts
Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 10, 2007 03:44 PM|LINK
Phil, twas a great idea to use using(ActionDisposable) for creating forms. I can see this being useful for a heap of other tags too.
is there something wrong with this snippet? my onsubmit handler isn't being rendered. Anyone else have the same problemo?
<%using (Html.Form<RemindersController>(a => a.Create(), FormExtensions.FormMethod.post, new { onsubmit="alert('');" })) { %>
<%}%>
I'm enjoying the ASP.NET world so much more now that I've got tight control of my HTML!
robconery
Participant
852 Points
195 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 10, 2007 05:43 PM|LINK
Thanks for reporting this - there indeed is a rendering bug that I just found (thanks to you!) where the passed in attributes aren't being written. I'll work to get a refreshed zip up as soon as I can - I'd like to wait and see if there are more bug reports.
CVertex
Member
147 Points
128 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 10, 2007 10:03 PM|LINK
Thanks Rob.
There's like a billion overloads I can understand how'd you'd miss one case.
While I've got your attention, I was hoping you could answer these questions:
1. Why do I have to prefix C# keywords with underscore for helper attributes (like new {_class="blah"}? Why not @ like senor C# allows?
2. I like the linq expression first parameter for typed ActionLinks/Forms, but I found the requirement to include a dummy parameter a bit confusing. e.g. using expression ac => ac.Create("name","blah","blah") on a Form<T>() will still correctly change the 3 arguments to the post fields. I found this cool but a bit confusing. It's like I have literals in my code that are just dummy place holders. Maybe I'm using the linq expression wrong.
3. I'd like to see a typed version of common attributes for HTML elements instead of always accepting object attributes. I know it'll clutter the code a bit, but it feels a bit weird passing anonymous types around. What effect does this have on performance? Having to use reflection all the time to get attributes? Is performance a concern, or is reflection quick enough? I was always encouraged to avoid reflection in rendering situations for performance reasons. I guess the tradeoff is worth it.
4. I wrote a simple event scheduling system for my friend as an exercise, but I got stuck on paging. Do you know if ScottGu has that PagedList<T> lying around? When he's not looking, can you jump on his box and nab it for me? That's if asking him nicely doesn't work.
Thanks,
-CVertex
robconery
Participant
852 Points
195 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 10, 2007 10:17 PM|LINK
Sure...
1) Cause I didn't know any better :) - you can do both if you like
2) I'd say you only want to use this type of Form constructor if those values are needed for the action. Tell me more about what you're trying to do - I think there's some confusion.
3) Reflection can indeed be "spendy" - but it depends what you're trying to do. In this case we just grab property info from Type() and that doesn't cost that much. In terms of "typed' arguments - it would really clutter up the signatures (there are tons of attributes we'd have to account for). We can, however, overload this to take some kind of hash/List<>, but then you're cluttering up your UI.
4) In terms of PagedList<T>: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
CVertex
Member
147 Points
128 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 10, 2007 10:36 PM|LINK
Thanks for your prompt reply.
1. Fair enough
2. I'd like to use a typed action builder for my search for
so I do
<%using (Html.Form<RemindersController>(a => a.Search("blank"))) { %>
<input type=text name="query" value="enter query here"/>
<input type=submit/>
<%}%>
If i press submit my RemindersController search action gets "enter query here" not "blank" . See what i mean about the unused literals?
3. If you guys think performance is a small price, then the syntactic neatness works out great for me.
4. Sweet! Thanks!
Haacked
Contributor
6901 Points
412 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 11, 2007 08:06 AM|LINK
In terms of performance, we do use reflection all over the place. We haven't yet performed our usual suite of stress and perf tests yet. That is definitely going to be tested before we ever release a 1.0.
If we do find hot spots that are slow, we will probably use LCG (Lightweight Code Generation) to speed things up.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
abombss
Member
575 Points
164 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 15, 2007 08:19 PM|LINK
MS MVC could add more generic expression so you could do something like this:
public RemindersController : Controller { public void Search(string query, int page) { } } Html.Form<RemindersController,string, int>( (c, q, p) => c.Search(q, p)))Its a little more terse but handles your dummy scenario by not using anything. I really haven't looked at at Html.Form enough to even know if this would work but I do know it works in Linq.MVC Linq
CVertex
Member
147 Points
128 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 16, 2007 05:26 AM|LINK
What's LCG? And how do I do that when I'm using performance critical reflection?
abombss
Member
575 Points
164 Posts
Re: Does Html.Form<T>(lambda,Method,attributes) ignore attributes for anyone else?
Dec 16, 2007 06:51 AM|LINK
Look at DynamicMethod. Its like reflection emit only faster without the overhead of creating a separate assembly. There are plenty of examples using it if you google around.