![](/img/trans.png)
[英]ASP.NET MVC4 WebGrid how to display all pages in the WebGrid
[英]How to display a JSON file in a webgrid without uploading it to the database using ASP.NET MVC?
我正在進行一個項目,我想上傳一個文件(不是真正上傳,而是通過上傳功能選擇),並在我的視圖中的webgrid中顯示該文件的數據。 到目前為止我得到了什么:
我的視圖上傳JSON文件:
上傳后,我正在提取此JSON文件的密鑰:
這個數據我想在我的視圖中顯示在WebGrid中而不將其上傳到數據庫,有誰知道如何做到這一點?
查看目前為止的代碼:
@model List<APIBrokerFrontend.Models.JsonModel>
@{
ViewBag.Title = "Api Broker";
if (Session["userID"] == null)
{
Response.Redirect("~/Login/Index");
}
var grid = new WebGrid(canPage: true, rowsPerPage: 10);
grid.Bind(source: Model, rowCount: ViewBag.TotalRows, autoSortAndPage: false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="col-lg-5 d-flex align-items-stretch">
<h2>API Broker</h2>
</div>
<div class="row" id="content1">
<div class="col-lg-6 d-flex align-items-stretch">
</div>
<!--Search field and button-->
<div class="col-sm-5 d-flex align-items-stretch" style="padding-top: 5%;">
@using (Html.BeginForm("Import", "Mapper", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group" style="padding-left: 12%;">
<p>JSON File: <input type="file" name="jsonFile" /></p>
<input type="submit" value="Import" />
</div>
}
</div>
<div class="row" id="content2">
<div class="col-lg-12 d-flex align-items-stretch">
@grid.Table(tableStyle: "table table-responsive table-striped table-bordered",
columns: grid.Columns(
grid.Column(columnName: "Key", header: "Keys", format:@<text><div class="" data-id="@item.Key" data-propertyname="Key">@item.Key</div></text>)
)
)
</div>
<!--Pagerlist for webgrid-->
<div class="col-lg-12 d-flex align-items-stretch">
@grid.PagerList(mode: WebGridPagerModes.All, paginationStyle: "pagination pagination-small pagination-right")
</div>
@Html.Hidden("dir", grid.SortDirection)
@Html.Hidden("col", grid.SortColumn)
</div>
</div>
</div>
</body>
</html>
控制器代碼:
public class MapperController : Controller
{
// GET: Mapper
public ActionResult Index()
{
return View();
}
public ActionResult Import(HttpPostedFileBase jsonFile)
{
if (!Path.GetFileName(jsonFile.FileName).EndsWith(".json"))
{
ViewBag.ErrorMessage = "No JSON file";
return View();
}
else
{
List<string> jsonData = new List<string>();
using (System.IO.StreamReader reader = new System.IO.StreamReader(jsonFile.InputStream))
{
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
JObject o2 = (JObject)JToken.ReadFrom(jsonReader);
List<string> keys = o2.Properties().Select(p => p.Name).ToList();
var data = keys;
return View(keys);
}
}
}
}
}
希望有人可以提供幫助,
提前致謝!
這取決於您希望數據的瞬態程度。 如果您沒有將其上傳到任何形式的存儲,我認為它是一個非常短期的用途。 因此,您可以做的是緩存數據,或者更容易將數據作為json存儲在TempData對象中,然后您可以從相同的TempData對象中檢索該數據以在相同或另一個MVC頁面中使用。
此外,如果您選擇將其存儲為json,則始終可以使用NewtonSoft將其解析為對象類型。 希望這可以幫助。 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.