views:

436

answers:

5
A: 

I'm not quite sure what you mean by 'strings'?

If you mean less HTML, then certainly, you could use CSS to layout your forms.

If you mean less C# code, then don't forget that this form is an edit form, so it is displaying editable data, rather than just presenting a blank form to enter data in.

samjudson
A: 

@samjudson By strings I was referring to the C# reference type. I was meaning is it really necessary to have the action in there as a sting or the property names.

Iv since read the string for the action name can be removed by using this approach:

<%using(Html.Form<HomeController>(action=>action.Index()))}%>
...
<%}%>
Dan
+2  A: 

I don't really like strings in my code, as it isn't possible to refactor. A nice way is to use Linq Expressions. If you get passed a model as ViewData you can use the following statement:

<%= ShowDropDownBox(viewData => viewData.Name); %>
...

public static string ShowDropDownList<T>(this HtmlHelper html, Expression<Action<T>> property)
{
    var body = action.Body as MethodCallExpression;
    if (body == null)
     throw new InvalidOperationException("Expression must be a method call.");
    if (body.Object != action.Parameters[0])
     throw new InvalidOperationException("Method call must target lambda argument.");
    string propertyName = body.Method.Name;
    string typeName = typeof(T).Name;

    // now you can call the original method
    html.Select(propertyName, ... );
}

I know the original solution is performing faster but I think this one is much cleaner.

Hope this helps!

ollifant
A: 

@Kleolb Thats almost too far the opposite way, having everything done in code would make it very hard for a UI developer\Designer to use.

Dan
A: 

I don't think there is so much more in your code. The only difference is that you don't have to specify the id of the textbox/ dropdownlist / etc. twice.

ollifant