HtmlAttributes on ASP.NET MVC 3 with Razor view in VB.Net

15,442

Solution 1

Just crossed all over the internet for the answer to this and tripped on the solution myself. Granted the site I'm working on isn't live yet, but so far this solution is working for me. Here is the exact line of code that I wrote and is working for me:

@Html.CheckBox("chb", item.isAuditor, New With {.disabled = "disabled",  .tabIndex = "3"})

That piece of code is in a for each loop which is processing each "item" is the list passed to the view by the controller.

Let me know if that works for you.

Solution 2

I was having the same issue. i was originally writing the code like this

@Html.DropDownListFor(Function(model) model.CostCentreID, ViewData("ListOfCostCentres"), New With {.size = "20"})

but this was not working. but the following way work, i guess because we need to typecast ViewData before we can use it.

@Html.DropDownListFor(Function(model) model.CostCentreID, CType(ViewData("ListOfCostCentres"), SelectList), New With {.size = "20"})
Share:
15,442
Besnik
Author by

Besnik

Updated on June 04, 2022

Comments

  • Besnik
    Besnik almost 2 years

    Can anybody tell me how do I add this htmlAttribute on MVC 3 Razor view?

    New With {.watermark = "sometext", .title = "sometext"}
    

    I tried the C# way but it's not working (@watermark) but it's not working.

    Thanks.

    UPDATE here is the current usage I am trying but it's not working.

      @Html.EditorFor(Function(model) model.FirstName, New With {.maxlength = "50"})
    

    This isn't working either

     @Html.EditorFor(Function(model) model.FirstName, New Object() {"maxlength=50"})
    

    Bear in mind that I am doing this on vb.net. PLEEASE HELP!