2

I have 2 class in Model

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
}

public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
}

I have a view that use both class and I need use html.TextBoxFor. I can create BigModel:

public class BigModel
{
    public User user;
    public Product product;
}

so in View:

@model BigModel
@Html.TextBoxFor(m=> m.user.UserName)
@Html.TextBoxFor(m=> m.product.ProductName)    

Or i can use different partial view and reander them. But they are not my favorite solution.

Isn't there another way? such as:

<p>    
    User Name:
    @Html.TextBoxFor<User>(u=> u.UserName)
</p>
<p>    
    Product Name:
    @Html.TextBoxFor<Product>(p=> p.ProductName)
</p>

3 Answers 3

2

Look at TextBoxFor method signature:

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression
)

It's impossible. to write:

@Html.TextBoxFor<Product>(p=> p.ProductName)

So you are "stuck" with this:

@Html.TextBoxFor(m=> m.product.ProductName)

(I don't know why you prefer the first version, it's even one char longer... =) )

Sign up to request clarification or add additional context in comments.

6 Comments

in second version add new class to model: BigModel. if i want use it I must add tens of class to model.
@MortezaTavakoli. You have to have that two classes in your Model, Where do you think the HTML helper takes the value from?
this is part of my page. i get value by ajax. i need html helper such as Html.LabelFor<User>(u=> u.UserName).
@MortezaTavakoli. And if there will error in the POST how the attempted value will be saved rendered?
it is not my problem. i get and set data by Ajax correctly. i only need to reander <label> and <input> tag by Html.LebelFor or Html.TextBoxFor
|
0

How exactly do you think you'll get the reference to User and Product that way? The generics only expose the TYPE of object, not the instance of the object. Even so, no generic versions of those exist.

I have seen several quesitons like this recently. What is the big deal with referencing the full type? Why do people have problems with this?

If you are passing your data view ViewBag, then you can simply do this:

@Html.TextBoxFor(m => ViewBag.Value)

However that's rather silly since a) you lose your model binding and b) you aren't gaining strong typing, so you might as well just use the non-strongly typed.

@Html.TextBox("Value", ViewBag.Value);

2 Comments

I get data by ViewBag or Ajax. i need html Helper such as: Html.LabelFor<User>(u=> u.UserName)
You aren't making any sense. No, you don't need an Html helper such as that. First, it doesn't exist. Second, it can't work. It's impossible for it to work that way, Generic methods can't function as you want them to.
0

With C#6 you might consider not using the @Html helper but just write inputs as plain html.

For example:

<input type="text" name="@($"{nameof(BigModel.User)}.{nameof(User.UserName)})" />

This is also handy when you are not using the same model for get as postback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.