我无法将部分视图返回给控制器。我已经能够成功地做到这一点,但似乎我在做两次这样做时遇到了麻烦。错误发生在代码的末尾。这是我的工作流程:
控制器名称:
public class PatientMergeController : Controller
页面加载,返回视图:
public ActionResult Index()
{
return View();
}
返回的视图是Index.cshtml:
@using ThisController = myNameSpace.Web.App.Controllers.PatientMerge.PatientMergeController
@model IEnumerable<myNameSpace.Repository.Model.PatientMerge>
@Scripts.Render(myNameSpace.Web.App.BundleConfig.GetVirtualPathForScript(myNameSpace.Web.App.BundleConfig.Scripts.PatientMerge))
@using System.Web.Helpers;
<div id="">
<h2>
Patient Merge
</h2>
<div class="container">
<div class="searchOneWrp">
<fieldset>
<legend><strong>Patient One Search</strong></legend>
First Name: <input type="text" id="fnamePone" />
Last Name: <input type="text" id="lnamePone" />
D.O.B.: <input type="text" id="dobPone" />
<button class="patient_look_up_button" id="btnPOneSearch" name="btnPOneSearch" type="submit" onclick="SearchOne()"
title="Search">
Search
</button>
</fieldset>
<div id="grdPatientOneSearch">
@{Html.RenderPartial("PatientOneSearch", Model);}
</div>
<div id="grdPatientOneOrderWrp">
@{Html.RenderPartial("PatientOneOrderDetails", Model);}
</div>
<div id="grdPatientOneRxWrp">
</div>
<div id="grdPatientOneNotesWrp">
</div>
</div>
<div class="searchTwoWrp">
<fieldset>
<legend><strong>Patient Two Search</strong></legend>
First Name: <input type="text" id="fnamePtwo" />
Last Name: <input type="text" id="lnamePtwo" />
D.O.B.: <input type="text" id="dobPtwo" />
<button class="patient_look_up_button" id="btnPTwoSearch" name="btnPTwoSearch" type="submit" onclick="SearchTwo()"
title="Search">
Search
</button>
</fieldset>
<div id="grdPatientTwoSearch">
@{Html.RenderPartial("PatientTwoSearch", Model);}
</div>
<div id="grdPatientTwoOrderWrp">
</div>
<div id="grdPatientTwoRxWrp">
</div>
<div id="grdPatientTwoNotesWrp">
</div>
</div>
</div>
</div>
这会加载一些搜索文本框。当用户执行搜索时,他们单击搜索按钮。搜索按钮执行此JS:
function SearchOne() {
var fname = $("#fnamePone").val();
var lname = $("#lnamePone").val();
var dob = $("#dobPone").val();
var url = "/PatientMerge/PatientSearch";
//post to server, expect partial view to be returned as html, update page
$.post(url, { 'fname': fname, 'lname': lname, 'dob': dob, pat_number: 1 })
.done(function (response) {
$("#grdPatientOneSearch").html(response);
});
}
调用控制器方法PatientSearch:
public PartialViewResult PatientSearch(string fname, string lname, string dob, int pat_number)
{
try
{
var target = new PatientRepository();
var result = target.GetPatient(fname, lname, dob, "", "", "");
List<myNameSpace.Repository.Model.PatientMerge> patientList = new List<myNameSpace.Repository.Model.PatientMerge>();
for (int i = 0; i < result.Count; i++)
{
myNameSpace.Repository.Model.PatientMerge patient = new myNameSpace.Repository.Model.PatientMerge();
patient.pat_id = result[i].PatientId;
patient.fname = result[i].FirstName;
patient.lname = result[i].LastName;
patient.phone_no = result[i].Phone;
patient.birth_date = result[i].DateOfBirth;
patient.AgeYears = result[i].Age;
patient.gender_cd = result[i].Gender;
patient.addr1 = result[i].Address1;
patient.addr2 = result[i].Address2;
patient.city = result[i].City;
patient.state_cd = result[i].State;
patientList.Add(patient);
}
if (pat_number == 1)
{
//perform patient one search query and pone view return
return PartialView("PatientOneSearch", patientList);
}
else
{
//perform patient two search query and pone view return
return PartialView("PatientTwoSearch", patientList);
}
}
catch (Exception ex)
{
myNameSpace.Repository.Model.PatientMerge patient = new myNameSpace.Repository.Model.PatientMerge();
List<myNameSpace.Repository.Model.PatientMerge> patientList = new List<myNameSpace.Repository.Model.PatientMerge>();
patient.fname = "Error";
patientList.Add(patient);
Logger.Log.Error(ex.Message + ex.StackTrace + ex.InnerException);
//return Json(new { error = ex.Message }, JsonRequestBehavior.AllowGet);
if (pat_number == 1)
{
//perform patient one search query and pone view return
return PartialView("PatientOneSearch", null);
}
else
{
//perform patient two search query and pone view return
return PartialView("PatientTwoSearch", null);
}
}
}
此控制器返回局部视图PatientOneSearch.cshtml
@using ThisController = myNameSpace.Web.App.Controllers.PatientMerge.PatientMergeController
@model IEnumerable<myNameSpace.Repository.Model.PatientMerge>
@{
myNameSpace.Repository.Model.PatientMerge pat = new myNameSpace.Repository.Model.PatientMerge();
}
@if (Model != null)
{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 20, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "grdContent");
grid.Pager(WebGridPagerModes.NextPrevious);
<div id="grdContent">
@grid.GetHtml(tableStyle: "webgrid PatientOneGrid",
headerStyle: "table-header",
alternatingRowStyle: "alt POneSearch",
selectedRowStyle: "select POneSearch",
rowStyle: "POneSearch",
columns: grid.Columns(
grid.Column("pat_id", "Patient ID"),
grid.Column("pat_status_cn", "Status"),
grid.Column("fname", "First Name"),
grid.Column("lname", "Last Name"),
grid.Column("birth_date", "DOB"),
grid.Column("AgeYears", "Age"),
grid.Column("gender_cd", "Gender"),
grid.Column("phone_no", "Phone"),
grid.Column("addr1", "Address 1"),
grid.Column("addr2", "Address 2"),
grid.Column("city", "City"),
grid.Column("state_cd", "State")
)
)
</div>
}
else
{
<label>No records found.</label>
}
上面的视图填充了网格。下面的JS正在等待行点击。
<script>
$('.POneSearch').on('click', function () {
SearchOneGetDetails(this);
});
</script>
点击行,执行此功能:
function SearchOneGetDetails(obj) {
var url = '/PatientMerge/OrderSearch';
var patid = $(obj).find('td:first').text();
$.post(url, { 'pat_id': patid , 'pat_number': 1 })
.done(function (response) {
$("#grdPatientOneOrderWrp").html(response);
});
}
调用控制器方法OrderSearch:
public PartialViewResult OrderSearch(int pat_id, int pat_number)
{
try
{
var target = new PatientRepository();
var result = target.GetPatientOrders(pat_id, "36500");
List<myNameSpace.Repository.Model.PatientMergeOrder> patientList = new List<myNameSpace.Repository.Model.PatientMergeOrder>();
for (int i = 0; i < result.Count; i++)
{
myNameSpace.Repository.Model.PatientMergeOrder patient = new myNameSpace.Repository.Model.PatientMergeOrder();
patient.DrugName = result[i].DrugName;
patient.InvoiceNBR = result[i].InvoiceNumber;
patient.LineStatus = result[i].LineStatusDescr;
patient.OrderDate = result[i].OrderDate.ToString();
patient.OrderNum = result[i].ParentOrderNo.ToString();
patient.PrimInsur = result[i].PrimaryInsurerName;
patient.SeconInsur = result[i].SecondaryInsurerName;
patient.ShipDate = result[i].ShipDate.ToString();
patient.StorePlusID = result[i].StoreId.ToString();
patient.TrackCode = result[i].TrackingCode;
patientList.Add(patient);
}
if (pat_number == 1)
{
//perform patient one search query and pone view return
return PartialView("PatientOneOrderDetails", patientList);
}
else
{
//perform patient two search query and pone view return
return PartialView("PatientTwoOrderDetails", patientList);
}
}
catch (Exception ex)
{
Logger.Log.Error(ex.Message + ex.StackTrace + ex.InnerException);
if (pat_number == 1)
{
//perform patient one search query and pone view return
return PartialView("PatientOneOrderDetails", null);
}
else
{
//perform patient two search query and pone view return
return PartialView("PatientTwoOrderDetails", null);
}
}
}
这里是问题发生的地方。我希望返回部分视图“PatientOneOrderDetails”。我的代码运行并输入if语句if(pat_number == 1)并进入执行部分返回的行,但从不进入该部分文件。我不认为MVC可以找到我的部分,但我不明白为什么。
我的视图结构是:
查看/ PatientMerge /...
这是预期视图返回的代码:
@using ThisController = myNameSpace.Web.App.Controllers.PatientMerge.PatientMergeController
@model IEnumerable<myNameSpace.Repository.Model.PatientMerge>
@{
myNameSpace.Repository.Model.PatientMerge pat = new myNameSpace.Repository.Model.PatientMerge();
}
@if (Model != null)
{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 20, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "grdOrderContentOne");
grid.Pager(WebGridPagerModes.NextPrevious);
<div id="grdOrderContentOne">
@grid.GetHtml(tableStyle: "webgrid",
headerStyle: "table-header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("add_date", "Order Date"),
grid.Column("order_id", "Order #"),
grid.Column("StorePlusID", "Order Store"),
grid.Column("LineStatus", "Line Status"),
grid.Column("invoice_nbr", "Script"),
grid.Column("drug_name", "Drug Name"),
grid.Column("PrimaryInsurance", "Primary Insurance"),
grid.Column("SecondaryInsurance", "Secondary Insurance"),
grid.Column("ship_date", "Ship Date"),
grid.Column("tracking_code", "UPS Tracking #")
)
)
</div>
}
else
{
<label>No records found.</label>
}
答案 0 :(得分:1)
模型定义是错误的,您期望部分中的myNameSpace.Repository.Model.PatientMerge并且您正在发送myNameSpace.Repository.Model.PatientMergeOrder