asp.net-mvc - 在主视图上调用带有表格的 Partialview
问题描述
我正在尝试使用某些项目的详细信息表调用部分视图。但是,局部视图在主视图中不显示任何记录。如何成功调用 Patrial 视图?部分观点如下:
@model IDECOHealthInsurance.Models.Pharmacy
@using (Html.BeginForm("pharmacyDetials", "Pharmacy"))
{
<h4>تفاصيل الصيدلية</h4>
<div id="dvPatientNotice" class="MainGridContainer pb-5">
@if (Model.dtItemsDetails != null)
{
<table dir="rtl" id="Paitents" class="MainGrid">
<thead>
<tr style="text-size-adjust:auto">
<th>
رقم الموظف
</th>
<th>
التاريخ
</th>
<th>
الوقت
</th>
<th>
المستفيدون
</th>
<th>
ملاحظات
</th>
<th>
الباركورد
</th>
<th>
أسم العينة
</th>
<th>
الكمية
</th>
<th>
السعر
</th>
</tr>
</thead>
<tbody>
@foreach (System.Data.DataRow row in Model.dtItemsDetails.Rows)
{
<tr style="width:100%">
<td>
@row["EMPLOYEE_NUMBER"]
</td>
<td>
@row["ENTRY_DATE"]
</td>
<td>
@row["ENTRY_TIME"]
</td>
<td>
@row["BENEFICIARIES"]
</td>
<td>
@row["NOTE"]
</td>
<td>
@row["ITEM_CODE"]
</td>
<td>
@row["ITEM_NAME"]
</td>
<td>
@row["QTY"]
</td>
<td>
@row["PRICE"]
</td>
</tr>
}
</tbody>
</table>
}
</div>
}
控制器如下:
[HttpGet]
public ActionResult pharmacyDetials(Pharmacy model)
{
var masterID = Convert.ToInt32(Session["login"]);
if (masterID == 0)
{
return RedirectToAction("Login");
}
else
{
Models.Pharmacy objPharamcyMode = new Pharmacy();
IDECOServiceReference.IdecoAPIServiceClient idecoAPI = new IDECOServiceReference.IdecoAPIServiceClient();
DataTable dataTable = idecoAPI.GETPHARMACYEMPLOYEEMASTER("", 1);
model.dtItemsDetails = dataTable;
return PartialView("_PharmacyDetails", model);
}
}
主视图如下:
@model IDECOHealthInsurance.Models.Pharmacy
@{
ViewBag.Title = "PharmacyApplication";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table style="height:680px; width:1280px; border:hidden">
<tr>
<td>
<div id="pDetail">
@Html.Partial("_PharmacyDetails", Model)
</div>
</td>
<td>
@using (Ajax.BeginForm("PharmacyApplication", "Pharmacy", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "updatePnl", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, LoadingElementId = "Loading", OnBegin = "" }))
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.PHARMACY_NAME, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.PHARMACY_NAME)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EMPLOYEE_NUMBER, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EMPLOYEE_NUMBER, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EMPLOYEE_NUMBER, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ENTRY_DATE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.ENTRY_DATE, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Today.ToString("dd/MM/yyyy"), @readonly = "readonly" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ENTRY_TIME, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Today.ToString("HH:mm:ss"), @readonly = "readonly" } })
<div class="col-md-10">
@Html.DisplayFor(model => model.ENTRY_TIME)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BENEFICIARIES, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
زوجة @Html.RadioButtonFor(m => m.BENEFICIARIES, 1)
أبن @Html.RadioButtonFor(m => m.BENEFICIARIES, 2)
أبنة @Html.RadioButtonFor(m => m.BENEFICIARIES, 3)
الموظف @Html.RadioButtonFor(m => m.BENEFICIARIES, 4)
@Html.ValidationMessageFor(model => model.BENEFICIARIES, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Note, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<textarea name="NOTE" id="comments" style="font-family:'Times New Roman';font-size:1.2em; width: 280px; height:auto" placeholder="أكتب ملاحظاتك هنا"></textarea>
@Html.ValidationMessageFor(model => model.Note, "", new { @class = "text-danger" })
</div>
</div>
<div id="showPnl">
<input class="btn btn-default" type="button" value="تفاصيل الصيدلية" onclick="@("window.location.href='" + @Url.Action("pharmacyDetials", "Pharmacy") + "'");" />
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="panel" value="أضافة" class="btn btn-default" />
</div>
<input class="btn btn-default" type="button" value="خروج" onclick="@("window.location.href='" + @Url.Action("LogOut", "Pharmacy") + "'");" />
</div>
</div>
}
</td>
</tr>
</table>
<div id="updatePnl">
@Html.Partial("_PartialPharmacyDetails", Model)
</div>
<br />
<br />
<br />
<div id="pnlItemsDetails">
@Html.Partial("_PartialItemsDetails", Model)
</div>
当我单击药房详细信息(阿拉伯语)=“تفصيل الصيدلية”按钮时,它会将我重定向到另一个页面,其中包含我想在主视图中显示的所需表格。但是,我不希望发生这种情况,我想在不单击按钮的情况下显示表格。你能解释一下为什么会出现这个问题吗?
运行时的结果如下所示:
应用程序在运行时的结果
解决方案
如果要防止页面重定向,则必须使用 ajax 请求。替换以下行:
<input class="btn btn-default" type="button" value="تفاصيل الصيدلية" onclick="@("window.location.href='" + @Url.Action("pharmacyDetials", "Pharmacy") + "'");" />
和 :
<input class="btn btn-default" type="button" value="تفاصيل الصيدلية" onclick="getPharmacyDetails();" />
并在您的 javascript 部分中,添加以下代码:
function getPharmacyDetails()
{
$.ajax({
url : '@Url.Content("~/Pharmacy/pharmacyDetials")',
type: "GET",
success: function (result) {
$("#pDetail").html(result);
}
});
}
另外,替换下面的 HTML :
<div id="pDetail">
@Html.Partial("_PharmacyDetails", Model)
</div>
和 :
<div id="pDetail"></div>
上面的 ajax 请求将执行部分视图并pDetial
用所需的结果填充 div。试试看,如果您有任何疑问,请告诉我。