我有一个MVC / asp.net Web应用程序,用户(除其他外)可以在其中修改数据库中的内容。一切工作正常,除了当我向网络网格中添加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的数据源。
我的问题是如何保留所有可用数据,当我单击以转到第二页时,它仅使用与以前相同的数据/列表?
注意事项:单击按钮时,我来自本地主机:##### / Content / ContentUpdate =>本地主机:##### / Content / ContentUpdate?page = 2至少从理论上讲应该是这样,但是在我到达那里之前会抛出错误。
注意:当为一行选择了“ select”时,我的javascript / JQuery仅将JSON传递给控制器。它什么也没做。
预先感谢!
答案 0 :(得分:0)
没人回答,所以我只是关闭了分页功能。
@model IEnumerable<Translation_Interface.Models.WebGrid>
@ {
WebGrid grid = new WebGrid(source: Model, canPage: false);
}