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.

M.Alnashmi
  • 582
  • 1
  • 4
  • 15
  • 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 ` –  May 20 '15 at 08: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) – M.Alnashmi May 20 '15 at 08:13

1 Answers1

-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)
Chameleon
  • 149
  • 2
  • 8
Kawsar Hamid
  • 514
  • 3
  • 13
  • what happens if model.Transactions is empty? – M.Alnashmi May 20 '15 at 09:27
  • will throw exception. Try @Html.LabelForm(model=>model.Transactions.FirstOrDefault().Property) – Kawsar Hamid May 20 '15 at 09:30
  • 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. – M.Alnashmi May 20 '15 at 09:39
  • Yes use FirstOrDefault(). This will work even if you dont pass the model to view. – Kawsar Hamid May 20 '15 at 09:39
  • 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? – M.Alnashmi May 21 '15 at 07:23