1

The code looks like this:

@Html.LabelFor(m => m.FormModel.Position, Model.Company.DefaultEmployee.ToStringOrDefault())

And the HTML looks like this:

<label for="FormModel.Position" >Programmer</label> ("Programmer" is the string generated by the Model.Company.DefaultEmployee.ToStringOrDefault() method)

I tried this:

@Html.LabelFor(m => m.FormModel.Position, Model.Company.DefaultEmployee.ToStringOrDefault(), new {id = "position-label"})

But it does not work.

This is what the Html extension looks like:

 public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
    {
        return LabelFor(html, expression, labelText, new RouteValueDictionary(htmlAttributes));
    }
  • Using `new {id = "position-label"}` works just fine (unless your still using MVC-2). What makes you think it does not work? –  Feb 28 '18 at 22:06
  • I get an error saying that the call is ambiguous between `LabelFor(this HtmlHelper html, Expression> expression, string labelText, object htmlAttributes)` and `LabelFor(this HtmlHelper html, Expression> expression, string labelText, IDictionary htmlAttributes)` – Andrea Fazakas Mar 01 '18 at 08:30
  • What your claiming does not make sense. You have said the 1st snippet generates `` - where does the `style="padding:0"` come from? And the text will not be `Model.Company.DefaultEmployee.ToStringOrDefault()` - it will be the string generated by that method. What is your `.ToStringOrDefault()` method and what is its output? –  Mar 01 '18 at 09:47
  • Yes, I forgot to take the padding out. While I was waiting for an answer i just wrote the label as instead of Html.LabelFor(...) so that I can make some modifications to it. As for Model.Company.DefaultEmployee.ToStringOrDefault() it generates "Programmer", but I thought that it would be a bit confusing if I just wrote "Programmer" instead of that method, but maybe I should have just written that it generates "Programmer". So, in other words, the generated HTML looks like this: ``. Sorry for all the confusion. – Andrea Fazakas Mar 01 '18 at 10:33

0 Answers0