Razor遇到一个问题,当我调用助手@EditorFor()时,HTML中的结果会生成一个带有name和id属性且每个值都不同的值。
<div class="form-group">
@Html.LabelFor(model => model.OCRDSAPF2.CardName, "Cliente", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OCRDSAPF2.CardName, new { htmlAttributes = new { @class = "form-control", @onBlur = "completarCodigo()", @value = "@ViewBag.cliente" } })
<script type="text/javascript">
$("#OCRDSAPF2_CardName").autocomplete({
source: function (request, response) {
$.ajax({
type: 'POST',
url: '@Url.Action("getClientes", "Cotizacions")',
data: { term: $("#OCRDSAPF2_CardName").val() },
datatype: "json",
success: function (result) {
response(result);
}
});
}
});
</script>
<strong>
@Html.ValidationMessageFor(model => model.OCRDSAPF2.CardName, "", new { @class = "bg-danger" })
</strong>
</div>
</div>
输出html:
<input class="form-control text-box single-line ui-autocomplete-input" data-val="true" data-val-required="ERROR:El campo Cliente no puede estar vacio." id="OCRDSAPF2_CardName" name="OCRDSAPF2.CardName" onblur="completarCodigo()" type="text" value="" autocomplete="off">
我想要:
<input class="form-control text-box single-line ui-autocomplete-input" data-val="true" data-val-required="ERROR:El campo Cliente no puede estar vacio." id="OCRDSAPF2_CardName" name="OCRDSAPF2_CardName" onblur="completarCodigo()" type="text" value="" autocomplete="off">
对不起我的英语