0

Ok im very new to ASP.NET 4.5 (giving PHP a break) and stuck with rendering a label.

I have a ViewModel that defines the necessary Models :

 public class TransactionsViewModel {
    public IEnumerable<Transaction> Transactions { get; set; }
    public IEnumerable<Area> Areas { get; set; }

    public IEnumerable<Mistake> Mistakes { get; set; }
    ...
 }

Transaction is just another model with simple properties, now i want to display a label for one of the Transaction properties.

My View has the strong type TransactionsViewModel

@model AuditSystem.ViewModels.TransactionsViewModel

This means that the HTMLHelper has type of < TransactionsViewModel> when overriding.

The method prototyope im trying to create is:

public static MvcHtmlString LabelFor<TModel, TClass, TValue>(
        this HtmlHelper<TModel> helper,
        IEnumerable<TClass> model,
        Expression<Func<TClass, TValue>> expression,
        object htmlAttributes
    )

Where < TModel> is TransactionsViewModel (strong typed) and < TClass> is (Transaction)

This causes a problem when trying to call helper.LabelFor since that expectes an expression of Expression>, but i need to pass Func.

I tried to reconstruct an expression with the valid types, but get stuck on the call to the html.labelfor and typecasting wont work.

2
  • Can you explain what you expect the output to be. Are you trying to create a label for the collection, or a property of an item in the collection. The first makes no sense since a <label> element is associated with a form control (clicking on its sets focus) and you can't have a form control for a collection Commented May 20, 2015 at 8:09
  • My ViewModel has a collection of Transaction. I want to display a label for Transaction.Property. I expect to call my override with: html.labelfor(Model.Transactions, t => t.property) Commented May 20, 2015 at 8:13

1 Answer 1

-1

I am not sure why do you need to override LabelFor. You might try like this-

@Html.LabelFor(model=>model.Transactions.First().Property)

Update

Use FirstOrDefault instead of First to avoid exception.

 @Html.LabelFor(model=>model.FirstOrDefault().Property)
Sign up to request clarification or add additional context in comments.

5 Comments

what happens if model.Transactions is empty?
will throw exception. Try @Html.LabelForm(model=>model.Transactions.FirstOrDefault().Property)
Yeah, i dont want to assume transactions always contains object because scenarios do exist where its empty. So i have to rebuild an expression that changes my TClass to a TModel so Html.Labelform can accept it. Problem will be TModel is a TransactionsViewModel and TClass is a Transaction.
Yes use FirstOrDefault(). This will work even if you dont pass the model to view.
I just thought this approach is hackish and tried to avoid it. I thought the best approach was an override. I was copying a function i found that did the same thing but for the DisplayFor. Not sure who downvoted but i hope they give a reason?

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.