以 json 形式返回部分视图?
Posted
技术标签:
【中文标题】以 json 形式返回部分视图?【英文标题】:return to partial view as json? 【发布时间】:2020-06-29 08:37:34 【问题描述】:我有 2 个视图“索引”和“_Track”。我需要将我的数据列表返回到“_Track”——这是部分视图。有人可以帮我,因为我收到了这个错误。我需要将数据列表返回到部分视图中。
错误 "for each statement cannot operation on variable type does not contain a public instance definition for getenumerator"
请参阅下面的参考代码。
父模型。
public class TrackingFormModel : BaseModel
public string TrackingNumber get; set;
public TrackingListModel TrackingList get; set;
儿童模型
public class TrackingListModel : BaseModel
public string Name get; set;
public string Departure get; set;
索引.cshtml
@using ChenvelMobile.Web.Models.Tracking
@model TrackingFormModel
@
ViewBag.Title = "index";
Layout = null;
@using (Html.BeginForm("index", "tracking", FormMethod.Post))
@Html.HiddenFor(x => x.Id)
<div class="MainContainer">
<div class="row">
<div class="form-group">
<div class="col-md-12">
<h4>Track an Item</h4>
<p>Hint: Enter the correct tracking number (Example. AU0002004)</p>
<div class="input-group">
@Html.TextBoxFor(x => x.TrackingNumber, new @class = "form-control", @id = "testSize", @placeholder = "Enter your box number..." )
<div class="input-group-btn">
<button class="btn btn-primary" type="submit" id="show">
<span class="glyphicon glyphicon-search btnSize" style="font-size: 20px"></span>
</button>
</div>
</div>
<br />
@Html.Partial("_Track", Model)
</div>
</div>
</div>
</div>
_Track.cshtml(部分视图)
@using ChenvelMobile.Web.Models.Tracking
@model TrackingListModel
@foreach (var item in Model)
<div>@item.Id</div>
<div>@item.Name</div>
<div>@item.Departure</div>
控制器
public JsonResult Index(TrackingFormModel model)
string date = String.Format("0:MM/dd/yyyy", DateTime.Now);
DateTime startdate = DateTime.Parse(date);
DateTime prevoiusdate = startdate.AddDays(-90);
var tracking = _cSI_DataService.Find(x => x.reciept_id == model.TrackingNumber).Where(x => Convert.ToDateTime(x.date_pl) > prevoiusdate).ToList();
var list = (from t in tracking
join d in _departureItemsTableService.GetAll() on t.box_id.Trim() equals d.BoxNo.Trim() into departitem from departitems in departitem.DefaultIfEmpty()
join dt in _departureTableService.GetAll() on departitems?.DepartureId equals dt.DepartureId into dep from depart in dep.DefaultIfEmpty()
select new TrackingListModel
Id = t.Id,
Name = t.firstname_s + " " + t.lastname_s,
Departure = depart?.DepartureDate ?? String.Empty,
);
return Json(new data = list.ToList() , JsonRequestBehavior.AllowGet);
【问题讨论】:
【参考方案1】:尝试将跟踪列表传递给您的局部视图。目前您传递的是 TrackingFormModel,而不是预期的 TrackingListModel。
@Html.Partial("_Track", Model.TrackingList)
【讨论】:
以上是关于以 json 形式返回部分视图?的主要内容,如果未能解决你的问题,请参考以下文章