Как я могу обновить частичный вид и основной вид одновременно?
1) Вот метод контроллера моего основного вида:
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) Таким образом, мой основной вид отображает веб-сетку, которая содержит в каждой строке идентификатор, имя иколичество его подимен, а также иконки изображений для добавления, редактированияи значок, который открывает частичное представление, которое отображает имя и все его подимены:
@{
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) Я дал этому изображению значок «class: open-sensor», который при нажатии открывает упомянутый частичный вид, вложенный в div «sensorNames», и вот сценарий java для этого действия:
$('.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) Затем метод контроллера фильтрует выбранный идентификатор:
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) И это открывает частичное представление внутри 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)
))
Вот часть вопроса - как мне удалить свое имя (конечно, в частичном представлении), используя значок изображения в третьем столбце «class = sensor-delete-table» И обновить эту сетку (вынуть удаленную строку ) а также основной вид веб-сетки (уменьшить количество вложенных имен на 1)?
6)Что мне удалось сделать был удалить запись в моей базе данных и обновить только частичный вид, но не основной вид:
$(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) Это метод удаления:
[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 });
}
Это успешно удаляет вложенное имя и переносит его в метод, который я упомянул в шаге 4), откуда оно переходит в «complete:» из шага 6). Вы можете спросить, почему я не использовал «success:» вместо «complete»? Поскольку он не регистрирует, что он что-то сделал, и ни один из них не завершает и не завершает. Я также попытался сделать "window.location.reload ()" и попытался поместить его перед "$ (" # sensorNames "). Html (result.responseText);" линия и любое другое место, о котором я мог подумать, но это всегда заканчивается в неправильном порядке, в котором я хочу. Сначала я хочу, чтобы он обновил страницу, а затем открыл новую веб-сетку с наименьшим суб-именем, но он только наоборот: удаляет удаленную и обновляет страницу, поэтому я остаюсь с обновленным основным видом без открытого частичное представление - если я не открою его вручную, но это не то, что я хочу.
Извините за длинный пост, я впервые задаю вопрос здесь.
8) Отредактируйте, чтобы показать полученный HTML, первая таблица:
<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>
Вот вторая таблица:
<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>