0

The title might be confusing, so here is my problem: I'd like in my View a readonly textbox only if my Action is for editing (something like EditLocation) and if my Action is intended for adding a new record (AddLocation), I'd like an editable textbox.

My ff. code works, but I was wondering if there's a "cleaner" solution

@using (Html.BeginForm(Model.Location.Id == 0 ? "AddLocation" : "EditLocation", "Location"))
    {
        <fieldset>
            @Html.HiddenFor(x => x.Location.Id)
            @Html.HiddenFor(x => x.Location.CompanyGroupId)
            @Html.HiddenFor(x => x.CompanyGroup.Id)
            @Html.HiddenFor(x => x.CompanyGroup.Code)

            <div class="form-group">
                <strong><span>@ResourcesCommon.Location_Code</span></strong>
                @if (Model.Location.Id == 0)
                {
                    @Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control" })
                    @Html.ValidationMessageFor(x => x.Location.Code)
                }
                else
                {
                    @Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control", @readonly = "readonly" })
                    @Html.ValidationMessageFor(x => x.Location.Code)
                }

            </div> ...

Thanks and have a good week ahead!

Caloyski
  • 328
  • 2
  • 6
  • 18
  • One option would be to create your own `HtmlHelper` extension methods ([refer this answer](http://stackoverflow.com/questions/26482305/access-model-class-instance-from-a-custom-additionalmetadataattribute-asp-net-m/26483022#26483022) for an example), but really, you should be using 2 views (and make use of partials for common html) –  Feb 22 '16 at 11:03
  • I totally agree with creating two separate views, but the owner of the application wants a single View for reusability purposes. – Caloyski Feb 22 '16 at 11:10
  • 1
    Seriously? Its a maintenance nightmare and difficult to debug. If they want reusability, then make use of partials for the common parts of the views. –  Feb 22 '16 at 11:15
  • I agree, voted your comment up :D – Caloyski Feb 24 '16 at 10:34

2 Answers2

2

This might help you:

@Html.TextBoxFor(x => x.Location.Code, new Dictionary<string, object>() .AddIf(true, "@class", "form-control") .AddIf(Model.Location.Id != 0, "@readonly", "readonly"))

// This returns the dictionary so that you can "fluently" add values
    public static IDictionary<TKey, TValue> AddIf<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, bool addIt, TKey key, TValue value)
    {
        if (addIt)
            dictionary.Add(key, value);
        return dictionary;
    }

I took it from another stackoverflow post long back which i don't have the link.

ManojAnavatti
  • 604
  • 1
  • 7
  • 18
0

You could do something like this (but it will produce more code):

@Html.TextBoxFor(x => x.Location.Code, new { @class = "form-control", @id="myInput" })

and then add some js:

$(document).ready(function() {
    $('#myInput').attr('readonly', @Html.Raw(Model.Location.Id != 0? "true" : "false"));
});
Nemanja Todorovic
  • 2,521
  • 2
  • 19
  • 30