Good day, colleagues. The project is built on .NET MVC5. The point here is, there is a form in which 3 buttons are implemented: one to start, the second to stop, the third to update. At start-up, you need to make the button become locked, and the stop button becomes active and when you return the message back to the Label, the string with the device name is not reset.
This script sends data to the controller for processing:
<script type="text/javascript"> function sendMessage(id, state) { switch (state) { case 'on': var isAlert = confirm("Вы уверенны, что хотите включить нагрузку?"); if (isAlert) { var name = "@Model.object_name"; var myform = document.getElementById('post_form'); myform.onsubmit = function () { document.getElementById('pusk_id').value = id; document.getElementById('pusk_state').value = 1; document.getElementById('object_name').value = name; var myButton_on = document.getElementById('puskatel1_on'); myButton_on.disabled = true; document.getElementById('puskatel1_on').value = 1; var myButton_off = document.getElementById('puskatel1_off'); myButton_off.disabled = false; document.getElementById('puskatel1_off').value = 0; myform.submit(); }; } break; case 'off': var isAlert = confirm("Вы уверены, что хотите отключить накрузку?"); if (isAlert) { var myform = document.getElementById('post_form'); myform.onsubmit = function () { document.getElementById('pusk_id').value = id; document.getElementById('pusk_state').value = 0; document.getElementById('object_name').value = "@Model.object_name"; var myButton_off = document.getElementById('puskatel1_off'); myButton_off.disabled = true; document.getElementById('puskatel1_off').value = 1; var myButton_on = document.getElementById('puskatel1_on'); myButton_on.disabled = false; document.getElementById('puskatel1_on').value = 0; myform.submit(); }; } break; case 'netral': var myform = document.getElementById('post_form'); myform.onsubmit = function () { document.getElementById('pusk_id').value = id; document.getElementById('object_name').value = "@Model.object_name"; myform.submit(); }; break; } } function stateButtonFotLoad() { // вызывается при старте, чтобы обновить состояние кнопок в Модели и сделать кнопки одна заблокирована другая нет. sendMessage(-1, 'netral'); document.getElementById("puskatel1_off").disabled = @Model.puskatel1_form_off; document.getElementById("puskatel1_on").disabled = @Model.puskatel1_form_on; } window.onload = stateButtonFotLoad; This form sends hidden fields using the POST method:
<div style="border: 1px solid #d6e9c6; float:left; width: 400px; margin-bottom:20px;"> <div class="modal-header"> <h4 class="modal-title">Панель управления нагрузкой для @Model.object_name</h4> </div> <div class="modal-body"> <form name="post_form" id="post_form" method="post" action="Partial" onsubmit="DoSubmit();"> @Html.Hidden("pusk_id", "0") @Html.Hidden("pusk_state", "") @Html.Hidden("object_name", "") @Html.Hidden("puskatel1_form_off", "true") @Html.Hidden("puskatel1_form_on", "false") <label style="margin-right: 15px; margin-bottom:15px;">Пускатель 1:</label> <button id="puskatel1_off" name="puskatel1_off" class="btn btn-danger" onclick="sendMessage(1, 'off')">Выключить</button> <button id="puskatel1_on" name="puskatel1_on" class="btn btn-success" onclick="sendMessage(1, 'on')">Включить</button><br> <label>Сообщение: <span>@Model.outputmessage</span></label><br> <label name="lblInfo"></label> <br> <button name="refresh" class="btn btn-primary" onclick="sendMessage(-1,'netral')">Обновить</button> </form> On the controller, different actions are performed on this data and the model is returned.
return View(post_form); // обычно так The idea is that when we go to another device, the update method for it is immediately called and the device is not reset in the line. The update function is written to the Status Model for buttons if true is blocking, false is not blocking.
Question: How to save states for presentation in the transmitted objects?
Processors, data from the form:
[HttpGet] public ActionResult Partial(string name) { send_device_message send = new send_device_message(); send.object_name = name; return View(send); } [HttpPost] public ActionResult Partial(send_device_message post_form) { if (post_form.pusk_id == 0) { post_form.outputmessage = "Действие отменено."; return View(post_form); } else if (post_form.pusk_id == -1) { string pusk1_cloud = post_form.factStartDevice; string pusk1_dev = post_form.realyStartDevice; RefreshStatusDevice(post_form); return View(post_form); } else { int state = Convert.ToInt32(post_form.pusk_state); string object_name = post_form.object_name; UpdatePuskToCloud(state, object_name); if (state == 0) { post_form.outputmessage = "Пускатель " + post_form.pusk_id + ", устройства " + post_form.object_name + " выключен."; } if (state == 1) { post_form.outputmessage = "Пускатель " + post_form.pusk_id + ", устройства " + post_form.object_name + " включен."; } if (state < 100) { send_message = "{\"Puskatel" + post_form.pusk_id + "\":" + state + "}"; send_object_name = post_form.object_name; System.Threading.ThreadPool.QueueUserWorkItem(a => SendCloudToDeviceMessage()); } return View(post_form); } } 
myform.onsubmit = function () { ... myform.submit(); }myform.onsubmit = function () { ... myform.submit(); }- wtf? - Igoronsubmit="DoSubmit();"?) And that's it. Then sometime, this handler is called (the form at this time is in the process of submit-a), and the formmyform.submit();is again initiated from itmyform.submit();. - Igor