Model Binding With Disabled Textbox

前端 未结 7 2034
余生分开走
余生分开走 2020-12-09 07:23

I have a textbox that I am defining as

<%= Html.TextBox(\"Username\", Model.Form.Username, 
        new { @class = \"textbox\", @disabled = \"disabled\" }         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 07:51

    Easiest way to submit disabled fields is to copy them over to an invisible, non disabled control before submit. Some people create those controls manually and hook up to the on change event in jQuery to copy them on demand, but this solution below is generic, easy and less chatty - although one rule: you must create (render) a clean page after postback (so

    $('#submitBtn').closest('form').one('submit', function() {
    
        var $form = $(this);
    
        // input, textarea, select, option, ----- button, datalist, keygen, output, optgroup
        $form.find('input:disabled, textarea:disabled, select:disabled, option:disabled').each(function () {
    
            var $item = $(this);
    
            var hiddenItem = $item.clone();
            hiddenItem.removeAttr('id');
            hiddenItem.removeAttr('disabled');
            hiddenItem.attr('style', 'display: none');
    
            $item.after(hiddenItem);
        });
    
    });
    

提交回复
热议问题