【发布时间】:2019-10-28 04:02:11
【问题描述】:
我有一个 MVC/asp.net Web 应用程序,用户(除其他外)可以在其中修改数据库中的内容。一切正常,除了当我向我的 webgrid 添加超过 10 个项目时,它会添加一个引发错误的第二个页面。
只是为了在代码中澄清一下,可以更新的内容属于一个应用程序,并且是用指定的语言编写的。
MyController: 总之,用户选择他们想要更新内容的应用程序和语言。然后我们连接到数据库并搜索与应用程序 ID 和语言 LCID 匹配的所有内容。如果匹配,则“工厂”将其作为对象传回,否则返回 null。如果为 null,则不会添加。如果有任何可用的内容,它会创建一个临时会话并将其传递给 View 控制器(这样 URL 会发生变化,而不仅仅是视图)。
public ActionResult Content(ContentCreator contentCreator)
{
consoleEntities db = new consoleEntities();
List<translation_contents> possibleContents = db.translation_contents.ToList(); //get a list of ALL the translation contents
List<WebGrid> webGrids = new List<WebGrid>(); //the passable object to populate the table in UpdateContent view
foreach (translation_contents contentToCheck in possibleContents)
{
//the FactoryOfWebGrids will check if the contentToCheck AppName and language match what the user provided.
//if it matches, it will pass back a webGrid object to add to the list. If it doesnt, it will pass back a Null which is not added.
WebGrid Factory = FactoryOfWebGrids(contentToCheck, contentCreator.SelectedApplication, contentCreator.CurrentLanguage);
if (Factory != null)
{
webGrids.Add(Factory);
}
}
//If there are no webgrids, do not go to the ContentUpdate View
if (webGrids.Count == 0)
{
ContentCreator passBack = CreateContentObject(); //needs a new ContentCreator object to reload the Contents View
ViewBag.Message = "No Content to Display. Please try again.";
return View("Content", passBack);
}
else
{
Session["webGrids"] = webGrids;
TempData["passableWebGrid"] = webGrids;
return Redirect("ContentUpdate");
}
}
public ActionResult ContentUpdate()
{
List<WebGrid> webGrids = TempData["passableWebGrid"] as List<WebGrid>;
return View(webGrids);
}
我的 HTML: 请注意,这是一个非常精简的版本,但我会将所有与 GridView 相关的内容都放入其中。
@model IEnumerable<Translation_Interface.Models.WebGrid>
@{
WebGrid grid = new WebGrid(source: Model);
}
....
<div class="table table-striped table-bordered" id="gridView">
@grid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: grid.Columns(
grid.Column(null, "Select", format:
@<text>@Html.ActionLink("Select", null, null, new { @class = "select" })</text>),
grid.Column("ContentTitle", "Title"),
grid.Column("ContentMin", "Content"),
grid.Column("CreatedBy", "Created By"),
grid.Column("LastUpdated", "Last Updated"),
grid.Column("Key", "ID")))
</div>
gridview 工作得非常好,内容作为 JSON 传递(此处未显示)就好了。但是当我点击进入第二页时,它会抛出一个错误:
System.InvalidOperationException: '必须先绑定数据源才能执行此操作。'
所以我相信正在发生的事情是它正在尝试重新加载视图,但在重新加载时,它丢失了 passableObject,因此它不再具有填充 webGrid 的数据源。
我的问题是如何保留所有可用的数据,当我点击进入第二页时,它只是使用与以前相同的数据/列表?
注意事项:我从 localhost:#####/Content/ContentUpdate => localhost:#####/Content/ContentUpdate?page=2 当按钮被点击(或至少理论上应该会发生这种情况,但是在我到达那里之前就会抛出错误)。
注意:我的 javascript/JQuery 仅在为一行选择“选择”时将 JSON 传递给控制器。它没有做任何其他事情。
提前致谢!
【问题讨论】:
标签: html asp.net-mvc razor pagination webgrid