我对编码和数据库的理解非常有限,所以我花了几个星期阅读教程和借用代码。在尝试自己编写之后,我又打了另一个砖墙并借用/改编了一些代码。我有一个名为' Sourcing Matrix'用一张名为“鱼”的桌子。我想要做的就是在数据库中搜索“Cod'并让它显示我们库存的任何Cod。我调整的代码是抛出一个错误,我无法弄清楚为什么,我已经尝试了一些不同的列名,重新安排代码,尝试谷歌,尝试了我有限的知识可能建议的一切。
当我点击尝试从default.cshtml转到搜索页面时,它会抛出错误。
这是我的搜索页面代码:
@{
Page.Title = "Filter WebGrid";
var db = Database.Open("Sourcing Matrix");
var query = "SELECT DISTINCT [Common Name] FROM Fish ORDER BY [Common Name]";
var Fish = db.Query(query);
query = "SELECT * FROM Fish WHERE [Common Name] LIKE @0 AND [Latin Name] LIKE @1";
var CommonName = "%" + Request["[Common Name]"] + "%";
var LatinName = "%" + Request["[Latin Name]"] + "%";
var data = db.Query(query, CommonName, LatinName);
var columns = new[]{"FishID", "[Common Name]", "[Latin Name]", "[Method of Production]", "[Catch Area]", "[Country of Production (Farmed)]", "[Area Detail]", "[Stock Detail]","[Method of Capture / Production]","Accreditation","[Certifying Body]","[MSC Rating]","[Other Rating]","[Do SHL Source?]","Reason"};
var grid = new WebGrid(data, columnNames: columns, rowsPerPage: 35);
}
<h1>Filter WebGrid</h1>
<form method="post">
<div id="grid">
Common Name: <input type="text" name="CommonName" value="@Request["[Common Name]"]" />
Latin Name: <select name="LatinName">
<option></option>
@foreach(var item in Fish){
<option @(Request["[Latin Name]"] == item.LatinName ? " selected=\"selected\"" : "")>@item.LatinName</option>
}
</select>
<input type="submit" />
@grid.GetHtml(
tableStyle : "table",
alternatingRowStyle : "alternate",
headerStyle : "header",
columns: grid.Columns(
grid.Column("FishID", "ID"),
grid.Column("CommonName", "[Common Name]"),
grid.Column("LatinName", "[Latin Name]"),
grid.Column("[Method of Production]"),
grid.Column("[Catch Area]"),
grid.Column("[Country of Production (Farmed)"),
grid.Column("[Area Detail]"),
grid.Column("[Stock Detail]"),
grid.Column("[Method of Capture / Production]"),
grid.Column("Accreditation"),
grid.Column("[Certifying Body]"),
grid.Column("[MSC Rating]"),
grid.Column("[Other Rating]"),
grid.Column("[Do SHL Source?]"),
grid.Column("Reason")
)
)
</div>
</form>
@section script{
<script type="text/javascript">
$(function(){
$('th a, tfoot a').live('click', function() {
$('form').attr('action', $(this).attr('href')).submit();
return false;
});
});
</script>
}
这是它引发的错误:
列名无效&#34; LatinName&#34;。描述:未处理的异常 在执行当前Web请求期间发生。请 查看堆栈跟踪以获取有关错误和位置的更多信息 它起源于代码。
异常详细信息:System.InvalidOperationException:无效的列 name&#34; LatinName&#34;。
来源错误:
第34行:第35行:
@foreach(Fish中的var项目){第36行:@ item.LatinName第37行:}第38行:
源文件:c:\ Users \ Cbond \ Documents \ My Web Sites \ EmptySite \ SFish2.cshtml Line:36
肯定有一个名为&#39; Latin Name&#39;的列,我只是无法理解为什么它不识别呢?!对不起,如果这是一个愚蠢的问题或很容易回答。就像我说仍然试图掌握这个编码业务!谢谢大家。 :)