我确实有一个MVC ASP.NET表单需要来自用户的数据,特别是用户有九个[必需]文本框来填充应有的信息,并且大多数时候这八个文本框的信息是相同的所以我让用户填写表单的所有字段,然后按一个重复按钮,允许用户只指定一个文本框的数据,其他八个文本框将填充之前指定的数据...但是用户不允许更改此重复信息,因此我使用jQuery
$("#idDireccion").attr("disabled", "disabled");
$("#idEvento").attr("disabled", "disabled");
$("#ddDepartamentos").attr("disabled", "disabled");
$("#ddMunicipios").attr("disabled", "disabled");
$("#idLugar").attr("disabled", "disabled");
$("#datepicker").attr("disabled", "disabled");
$("#idHoras").attr("disabled", "disabled");
$("#idPrecio").attr("disabled", "disabled");
并通过ViewBag指定重复值
@Html.EditorFor(model => model.lugarEvento, new { htmlAttributes = new { @class = "form-control left-border-none", @title = "especifique lugar en que impartirá el evento", @id = "idLugar", @Value = ViewBag.lugarEvento } })
我为八个文本框执行此操作,它显示信息并且不允许用户更改该信息,但是当用户将表单提交给处理数据库操作的控制器时,不会从复制中接收任何内容info(是ViewModel的一部分)所有值都为null。
我的问题是我必须做的是为了不让用户修改重复的信息并仍然在我的控制器中接收重复的信息。
答案 0 :(得分:1)
不要使用disabled属性而是使用readonly属性。这会将值发送给控制器。
$("#idDireccion").attr('readonly', true);
$("#idEvento").attr('readonly', true);
$("#ddDepartamentos").attr('readonly', true);
$("#ddMunicipios").attr('readonly', true);
$("#idLugar").attr('readonly', true);
$("#datepicker").attr('readonly', true);
$("#idHoras").attr('readonly', true);
$("#idPrecio").attr('readonly', true);