There is a strange problem that I don’t even know how to solve.

In View, I have a field that is filled with an ajax request, when choosing a drop-down menu of the company.

<div class="form-horizontal"> <div class="form-group"> <span class="control-label col-sm-3">Выберите компанию</span> <div class="col-sm-9"> @Html.DropDownListFor(model => model.CompanyId, ViewBag.SelectedCompanies as SelectList, "Выберите компанию", new { @class = "form-control", @id = "companies" }) </div> </div> </div> <div class="form-horizontal"> <div class="form-group"> @Html.LabelFor(m => Model.Ue_inv_number, new { @class = "control-label col-sm-3" }) <div class="col-sm-9"> @Html.TextBoxFor(m => m.Ue_inv_number, new { @class = "form-control required", @id = "inv_num" }) <div> @Html.ValidationMessageFor(m => m.Ue_inv_number) </div> </div> </div> </div> 

Request is done, here is the jquery code of the request

 function invent() { $("#inv_num").prop("disabled", true); $("#companies").change(function () { if ($("#companies").val() != "") { var options = {}; options.url = "/Uchet/GetInventNumber"; options.type = "POST"; options.data = JSON.stringify({ company: $("#companies").val() }); options.dataType = "json"; options.contentType = "application/json"; options.success = function (company) { $("#inv_num").empty(); for (var i = 0; i < company.length; i++) { $("#inv_num").val(company[i].Short_Name + company[i].id_num); } }; $.ajax(options); } else if ($("#companies").val() == "") { $("#inv_num").val(""); $("#inv_num").prop("disabled", true); } } )}; 

In the controller, it looks like this

 [HttpPost] public JsonResult GetInventNumber(int company) { int id_num; try { id_num = (from ue in db.Ue orderby ue.Id descending select ue.Id).First(); id_num++; } catch { id_num = 0; } var result_list = from c in db.Ue_companies.Where(x => x.Id == company) select new { c.Short_Name, id_num }; return Json(result_list); } 

All OK. the field is filled below Invent. number.

enter image description here

Further, when you click create, there is a problem. I put a full stop on the controller that accepts the model. enter image description here

That is, that this field is filled, he does not see! I can not explain why. Most likely because it is filled with an ajax request? How do I solve this problem?

Found the solution all deal $("#inv_num").prop("disabled", true); removed it from the request and the data is transferred

That is, you expose this property to the field, then it is disabled and now whatever data you would enter there, they are essentially not there. Can someone explain why?

  • I have a small question. You gave the code of the GetInventNumber method, and the screen attached another method. How to understand this? - Denis Bubnov
  • Can you post the code that sends the form? - Serafim Prozorov
  • There's just a submit button and that's it - shatoidil

1 answer 1

the contents of the disabled form fields are not included in the post request by the browser. To prevent editing the field, you need to use the non- disabled and readonly attribute.