Como atualizar a exibição parcial e a exibição principal ao mesmo tempo?
1) Aqui está o método do controlador da minha visão principal:
public ActionResult PredefPageLoad()
{
List<PredefineViewsView> predefViewsViews = null;
try
{
using (PanSenseEntities context = new PanSenseEntities())
{
int userID = Convert.ToInt32(Session["LoggedUserID"]);
predefViewsViews = (from x in context.PredefineViewsViews where x.UserID == userID select x).ToList();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return View(predefViewsViews);
}
2) Portanto, minha visão principal é exibir uma grade da web que contém em cada linha um ID, um nome e umquantidade de sub-nomes, bem como ícones de imagem para adicionar, editare um ícone que abre uma visualização parcial que exibe o nome e todos os seus subnomes:
@{
var grid = new WebGrid(Model);
}
<div id="gridContent">
<button type="button" id="createNewPredefinedView" style="margin-bottom: 20px;">@Html.Localize("createNew")</button>
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
mode: WebGridPagerModes.All,
columns:
grid.Columns(
grid.Column("PredefID", format: @<text> <span class="display-mode" id="PredefViewID">@item.PredefineViewID </span> <label id="PredefID" class="edit-mode">@item.PredefineViewID</label> </text>, style: "col1Width"),
grid.Column("", format: @<text>
<img class="edit-table display-mode click_images" src="~/Images/edit.png" />
<img class="delete-table display-mode click_images" src="~/Images/delete.png" />
<img class="open-sensors display-mode click_images" src="~/Images/Sensor.png" />
<img class="save-table edit-mode click_images" src="~/Images/save.png" />
<img class="cancel-table edit-mode click_images" src="~/Images/cancel.png" />
</text>, style: "col2Width", canSort: false),
grid.Column("PredefName", @Html.Localize("name").ToString(), format: @<text> <span class="display-mode"> <label id="lblPredefName">@item.Name</label> </span> <input type="text" id="PredefName" value="@item.Name" class="edit-mode" /> </text>, style: "col1Width", canSort: false),
grid.Column("AmountOfSensors", @Html.Localize("amountOfSensors").ToString(), format: @<text> <span class="display-mode" id="lblAmountOfSensors"> @item.SensorNo </span> <label id="AmountOfSensors" class="edit-mode"> @item.SensorNo </label> </text>, style: "col2Width", canSort: false)
))
</div>
<div id="sensorNames">
</div>
3) Eu dei a este ícone de imagem "class: open-sensores" que, quando clico, abre a vista parcial mencionada aninhada dentro da div "sensorNames", e aqui está o script java para essa ação:
$('.open-sensors').on('click', function () {
var tr = $(this).parents('tr:first');
var PredefID = tr.find("#PredefID").html();
var divSensorNames = $("#sensorNames");
var urlShowSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefID" })";
urlShowSensors = urlShowSensors.replace("PredefID", PredefID);
$(divSensorNames).load(urlShowSensors);
});
4) O método do controlador filtra o ID selecionado:
public ActionResult ShowSensorNames(string predefinedViewID)
{
List<PredefineViewItemsView> predefViewItemsViews = null;
try
{
using (PanSenseEntities context = new PanSenseEntities())
{
int predefViewID = Convert.ToInt32(predefinedViewID);
predefViewItemsViews = (from x in context.PredefineViewItemsViews where x.PredefineViewID == predefViewID select x).ToList();
}
}
return PartialView("DisplaySensors", predefViewItemsViews);
}
5) E abre a vista parcial dentro da div "sensorNames":
@{
var gridSensors = new WebGrid(Model, canPage:false);
}
@gridSensors.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
mode: WebGridPagerModes.All,
columns:
gridSensors.Columns(
gridSensors.Column("PredefineViewID", format: @<text> <span id="PredefineViewID">@item.PredefineViewID </span> </text>, style: "col1Width"),
gridSensors.Column("PredefineViewsItemID", format: @<text> <span id="PredefineViewsItemID">@item.PredefineViewsItemID </span> </text>, style: "col1Width"),
gridSensors.Column("", format: @<text><img class="sensor-delete-table click_images" alt="@Html.Localize("delete")" title="@Html.Localize("delete")" src="~/Images/delete.png" /></text>, style: "col1Width", canSort: false),
gridSensors.Column("PredefName", @Html.Localize("name").ToString(), format: @<text> <span><label id="lblPredefineViewName">@item.PredefineViewName</label> </span> </text>, style: "col1Width", canSort: false),
gridSensors.Column("PredefName", @Html.Localize("sensorName").ToString(), format: @<text> <span><label id="lblSensorName">@item.SensorName</label> </span> </text>, style: "col3Width", canSort: false)
))
Aqui está a parte da pergunta: como excluo meu subnome (dentro da exibição parcial, é claro) usando o ícone de imagem na terceira coluna "class = sensor-delete-table" E atualizo esta grade da Web (retire a linha excluída ), bem como a grade da web da visualização principal (diminua o número de sub-nomes em 1)?
6)O que eu consegui fazer foi excluir o registro no meu banco de dados e atualizar apenas a exibição parcial, mas não a exibição principal:
$(function () {
$(".sensor-delete-table").on("click", function () {
var divSensorNames = $("#sensorNames");
var tr = $(this).parents('tr:first');
var PredefineViewsItemID = tr.find("#PredefineViewsItemID").html();
var PredefineViewID = tr.find("#PredefineViewID").html();
var flag = confirm('@Html.Localize("deleteCheck")');
var urlShowNewSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefineViewID" })";
urlShowNewSensors = urlShowNewSensors.replace("PredefineViewID", PredefineViewID);
if (PredefineViewID != "" && flag) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: '@Url.Action("DeleteSensor", "PredefinedViews")',
data: JSON.stringify({ pviID: PredefineViewsItemID, pID: PredefineViewID}),
dataType: "json",
complete: function (result) {
$("#sensorNames").html(result.responseText);
},
});
}
});
});
7) Este é o método de exclusão:
[HttpPost]
public ActionResult DeleteSensor(int pviID, int pID)
{
using (PanSenseEntities context = new PanSenseEntities())
{
tblPredefineViewItem existing = context.tblPredefineViewItems.Find(pviID);
if (existing != null)
{
context.tblPredefineViewItems.Remove(existing);
context.SaveChanges();
}
}
return RedirectToAction("ShowSensorNames", "PredefinedViews", new { predefinedViewID = pID });
}
Isso exclui com êxito o subnome e o leva para o método que eu mencionei na etapa 4) de onde ele entra no "complete:" da etapa 6). Você pode perguntar por que não usei "success:" em vez de completo? Porque não registra que ele fez alguma coisa e nem "termina:" nem "termina:". O que eu tentei também foi fazer um "window.location.reload ()" e tentei colocá-lo antes de "$ (" # sensorNames "). Html (result.responseText);" linha e qualquer outro lugar em que eu pudesse pensar, mas sempre termina na ordem errada que eu quero. Primeiro, eu quero que ele atualize a página e abra a nova webgrid com um subnome a menos, mas faz o oposto - remove a excluída e atualiza a página, então fico com uma visualização principal atualizada sem uma abertura vista parcial - a menos que eu a abra manualmente, mas não é isso que eu quero.
Desculpe pelo longo post, esta é a minha primeira vez fazendo uma pergunta aqui.
8) Edite para mostrar o html resultante, a primeira tabela é:
<div id="gridContent" class="table-responsive col-md-6" style="padding-top: 20px; ">
<button type="button" id="createNewPredefinedView" style="margin-bottom: 20px;">Create new</button>
<table class="webgrid-table">
<thead>
<tr class="webgrid-header">
<th scope="col" style="display: none;">
<a href="/PredefinedViews/PredefPageLoad?sort=PredefID&sortdir=ASC">PredefID</a>
</th>
<th scope="col">
</th>
<th scope="col">
Name
</th>
<th scope="col">
Amount of Sensors
</th>
</tr>
</thead>
<tbody>
<tr class="webgrid-row-style">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">18 </span> <label id="PredefID" class="edit-mode" style="display: none;">18</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">pokusaj4555</label> </span> <input type="text" id="PredefName" value="pokusaj4555" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 10 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 10 </label> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">19 </span> <label id="PredefID" class="edit-mode" style="display: none;">19</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">pokusaj1333</label> </span> <input type="text" id="PredefName" value="pokusaj1333" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 11 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 11 </label> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">20 </span> <label id="PredefID" class="edit-mode" style="display: none;">20</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">pokusaj3</label> </span> <input type="text" id="PredefName" value="pokusaj3" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 10 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 10 </label> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">21 </span> <label id="PredefID" class="edit-mode" style="display: none;">21</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">asdasd</label> </span> <input type="text" id="PredefName" value="asdasd" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 3 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 3 </label> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">22 </span> <label id="PredefID" class="edit-mode" style="display: none;">22</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">qwer</label> </span> <input type="text" id="PredefName" value="qwer" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 3 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 3 </label> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">23 </span> <label id="PredefID" class="edit-mode" style="display: none;">23</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">asd123</label> </span> <input type="text" id="PredefName" value="asd123" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 4 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 4 </label> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">1052 </span> <label id="PredefID" class="edit-mode" style="display: none;">1052</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">qwerty</label> </span> <input type="text" id="PredefName" value="qwerty" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 0 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 0 </label> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width" style="display: none;"> <span class="display-mode" id="PredefViewID">2052 </span> <label id="PredefID" class="edit-mode" style="display: none;">2052</label> </td>
<td class="col2Width">
<img class="edit-table display-mode click_images" alt="Edit" title="Edit" src="/Images/edit.png">
<img class="delete-table display-mode click_images" alt="Delete" title="Delete" src="/Images/delete.png">
<img class="open-sensors display-mode click_images" alt="View of Existing Sensors" title="View of Existing Sensors" src="/Images/Sensor.png">
<img class="save-table edit-mode click_images" alt="Save" title="Save" src="/Images/save.png" style="display: none;">
<img class="cancel-table edit-mode click_images" alt="Cancel" title="Cancel" src="/Images/cancel.png" style="display: none;">
</td>
<td class="col1Width"> <span class="display-mode"> <label id="lblPredefName">qwerty</label> </span> <input type="text" id="PredefName" value="qwerty" class="edit-mode" style="display: none;"> </td>
<td class="col2Width"> <span class="display-mode" id="lblAmountOfSensors"> 0 </span> <label id="AmountOfSensors" class="edit-mode" style="display: none;"> 0 </label> </td>
</tr>
</tbody>
</table>
Aqui está a segunda tabela:
<table class="webgrid-table">
<thead>
<tr class="webgrid-header">
<th scope="col">
<a href="/PredefinedViews/ShowSensorNames?predefinedViewID=20&sort=PredefineViewID&sortdir=ASC">PredefineViewID</a>
</th>
<th scope="col">
<a href="/PredefinedViews/ShowSensorNames?predefinedViewID=20&sort=PredefineViewsItemID&sortdir=ASC">PredefineViewsItemID</a>
</th>
<th scope="col">
</th>
<th scope="col">
Name
</th>
<th scope="col">
Sensor Name
</th>
</tr>
</thead>
<tbody>
<tr class="webgrid-row-style">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">42 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Solar radiation </label> </span> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1123 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Soil Temperature </label> </span> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1124 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Soil Moisture na 15cm</label> </span> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1125 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Soil Moisture na 30cm</label> </span> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1126 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Leaf wetness prvi</label> </span> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1127 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Ait temperature </label> </span> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1128 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Air humidity </label> </span> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1129 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Wind speed </label> </span> </td>
</tr>
<tr class="webgrid-row-style">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1130 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Wind direction </label> </span> </td>
</tr>
<tr class="webgrid-alternating-row">
<td class="col1Width"> <span id="PredefineViewID">20 </span> </td>
<td class="col1Width"> <span id="PredefineViewsItemID">1131 </span> </td>
<td class="col1Width"><img class="sensor-delete-table click_images" alt="Delete" title="Delete" src="/Images/delete.png"></td>
<td class="col1Width"> <span><label id="lblPredefineViewName">pokusaj3</label> </span> </td>
<td class="col3Width"> <span><label id="lblSensorName">Precipitation </label> </span> </td>
</tr>
</tbody>
</table>