What is the best way to write a form in ASP.NET MVC?

What is the best way to write a form to submit some data to ASP.NET MVC? Is this how Scott Guthrie demonstrates here? Are there any better approaches? Perhaps with less use of strings?

alt text

+4
source share
1 answer

I don't like the lines in my code, as the refactor fails. A good way is to use Linq expressions. If you get the 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 that the original solution works faster, but I think it is much cleaner.

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1276383/


All Articles