简体   繁体   中英

Conditionally disabling input fields with radio buttons in ASP.NET MVC 4 Razor (should I just use jquery?)

I'm trying to disable an input field when a certain radio button is picked and I can't figure out how to do it cleanly.

I've seen several places where people have suggested using jQuery and I'm sure it would be easy, but isn't the whole point of using C# to not use javascript?

This is the code I'm working on

<div>
    @Html.TextBoxFor(model => model.files, 
        new 
        { 
            @class = "form-control", 
            type = "file", multiple = "true", 
            placeholder = "upload files"@*, 
            disabled = ?????*@ 
        })
</div>

<div class="form-inline" role="form">
    @Html.RadioButtonFor(model => model.editFiles, "no change", 
        new { @checked = "checked" }) Do not change files
    @Html.RadioButtonFor(model => model.editFiles, "add") Add new files
    @Html.RadioButtonFor(model => model.editFiles, "delete") Delete old files
    @Html.RadioButtonFor(model => model.editFiles, "replace") Replace old files 
    with new files
</div>

No, using C# is not to eliminate JavaScript. They serve two very different purposes. JavaScript is for the client side and should be used in situations (like yours) where you want a responsive user experience as it doesn't need to make round-trips to the server (slow and expensive).

For heavy lifting of data / backend processes that is where you would use C# and the server. It is there to do your SERVER work and return what is needed to the client. JavaScript then runs on the CLIENT to make the experience as nice as possible. However, you can mix the two when making async calls to get some data etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM