Eoin Casey
Eoin Casey

Reputation: 31

@HtmlTextBox Readonly attribute in Razor

Just having a problem with the @HtmlTextBox on my Razor page.

I want the box to be populated from another @HtmlTextBox, and not be editable as I want these to be exported to an excel file.

<table style="border:none">
    <tr>
        <td><label class="editor-label">Start Date:</label></td>
        <td>@Html.TextBox("StartDate", new { @readonly = "readonly" })</td>
            @*<td>@Html.TextBox("StartDate"), new { @class = "date-picker" })</td>*@
    </tr>
    <tr>
        <td><label class="editor-label">End Date:</label></td>
    <td>@Html.TextBox("EndDate", new { @readonly = "readonly" })</td>
    </tr>
    <tr>
        <td><label class="editor-label">Supplier Company Name</label></td>
        <td>@Html.TextBox("Company")</td>
    </tr>
</table>

With this, it just has "@readonly = "readonly"" in my search box which I don't want and it is still editable.

Upvotes: 1

Views: 2736

Answers (2)

Laxman Gite
Laxman Gite

Reputation: 2318

You have to add null parameter in your HTML helper :

Now your Helper Should be like this :

@Html.TextBox("EndDate",null, new { @readonly = "readonly" })


And your code will be look like this :

<table style="border:none">
            <tr>
            <td><label class="editor-label">Start Date:</label></td>
            <td>@Html.TextBox("StartDate",null, new { @readonly = "readonly" })</td>
                @*<td>@Html.TextBox("StartDate"), new { @class = "date-picker" })</td>*@
        </tr>
            <tr>
                <td><label class="editor-label">End Date:</label></td>
            <td>@Html.TextBox("EndDate", null,new { @readonly = "readonly" })</td>
            </tr>
            <tr>
                <td><label class="editor-label">Supplier Company Name</label></td>
                <td>@Html.TextBox("Company")</td>
            </tr>
        </table>

Cheers !!

Upvotes: 1

Andrei
Andrei

Reputation: 56688

You are using the wrong overload. TextBox(String, object) expects values as a second parameter.

What you want is TextBox(String, object, object), where third parameter is for HTML attributes:

@Html.TextBox("EndDate", "", new { @readonly = "readonly" })

Upvotes: 4

Related Questions