views:

46

answers:

1

I use ASP.NET MVC for serving web application and I want to create something like the following code.

<% using(HTML.Form(Model)) { %>
<%     HTML.CreateTextBox('txt1', x => x.Property1);
<% }

From the above code, Form extension method will receive object that represent type of current Model object in current View page. Next, CreateTextBox method will receive type from Form method and I can bind textbox to some property of this model.

Update 1

The following code is code of CreateTextBox method that will create instance of TextBox class.

public static CreateTextBox(string value, Expression<Func<object>> bindedProperty)
{
    // T should be receive from HTML.Form method or class
    return new TextBox<T>(value);
}

Is it possible to creating some code that doing something like the above code?

Thanks,

A: 

It's not necessary. The model is always going to be available, so you will always be able to do this:

<% 
using(Html.BeginForm()) 
{
    Html.TextBox('txt1', Model.Property1);
} 
%>

The Model property on your page is always typed (assuming you are using System.Web.Mvc.ViewPage<T>) so you will always have access to the properties directly through the Model property.

If you aren't using the generic version (System.Web.Mvc.ViewPage<T>) then you should probably use that instead of the non-generic version.

casperOne
I mean I want to set generic type of TextBox control that is generated by Html.TextBox method. Please read my updated question.
Soul_Master