我的网络网格有3列,所有3列都有下拉列表。我正在克隆最后一行并将其附加到webgrid。它追加,并且数据也绑定到新追加的行。但是显示了视图错误“必须先绑定数据源,然后才能执行此操作”。我正在共享代码。 任何帮助表示赞赏...
模型
public class HeaderInfo
{
public List<Header> HeaderModel { get; set; }
}
public class Header
{
public SelectList Xaxis { get; set; }
public SelectList Yaxis { get; set; }
public SelectList Zaxis { get; set; }
}
public class XStatus
{
public int ID { get; set; }
public string StatusName { get; set; }
}
public class YStatus
{
public int ID { get; set; }
public string StatusName { get; set; }
}
public class ZStatus
{
public int ID { get; set; }
public string StatusName { get; set; }
}
控制器
public class GraphController : Controller
{
// GET: Graph
public ActionResult Index()
{
// HeaderInfo h = new HeaderInfo();
PlotModel m = new PlotModel();
m.h.HeaderModel = HeaderDetails();
return View(m);
}
public List<Header> HeaderDetails()
{
List<Header> obj = new List<Header>();
obj.Add(new Header { Xaxis = GetXfield(), Yaxis = GetYfield(), Zaxis = GetZfield() });
return obj;
}
public SelectList GetXfield()
{
List<XStatus> status = new List<XStatus>();
status.Add(new XStatus { ID = 1, StatusName = "aaa" });
status.Add(new XStatus { ID = 2, StatusName = "aaa" });
status.Add(new XStatus { ID = 3, StatusName = "aaa" });
status.Add(new XStatus { ID = 4, StatusName = "bbb" });
status.Add(new XStatus { ID = 5, StatusName = "aaa" });
status.Add(new XStatus { ID = 6, StatusName = "nnn" });
status.Add(new XStatus { ID = 7, StatusName = "mmm" });
SelectList obj = new SelectList(status, "ID", "StatusName");
return obj;
}
public SelectList GetYfield()
{
List<XStatus> status = new List<XStatus>();
status.Add(new XStatus { ID = 1, StatusName = "aaa" });
status.Add(new XStatus { ID = 2, StatusName = "aaa" });
status.Add(new XStatus { ID = 3, StatusName = "aaa" });
status.Add(new XStatus { ID = 4, StatusName = "bbb" });
status.Add(new XStatus { ID = 5, StatusName = "aaa" });
status.Add(new XStatus { ID = 6, StatusName = "nnn" });
status.Add(new XStatus { ID = 7, StatusName = "mmm" });
SelectList obj = new SelectList(status, "ID", "StatusName");
return obj;
}
public SelectList GetZfield()
{
List<XStatus> status = new List<XStatus>();
status.Add(new XStatus { ID = 1, StatusName = "aaa" });
status.Add(new XStatus { ID = 2, StatusName = "aaa" });
status.Add(new XStatus { ID = 3, StatusName = "aaa" });
status.Add(new XStatus { ID = 4, StatusName = "bbb" });
status.Add(new XStatus { ID = 5, StatusName = "aaa" });
status.Add(new XStatus { ID = 6, StatusName = "nnn" });
status.Add(new XStatus { ID = 7, StatusName = "mmm" });
SelectList obj = new SelectList(status, "ID", "StatusName");
return obj;
}
查看
<script type="text/javascript">
$('#clickToAdd').click(function () {
var newrow = $("#grid1 tbody tr:last").clone();
$("#grid1 tbody").append(newrow);
})
</script>
@{
ViewBag.Title = "title";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid g_style = new WebGrid(source:Model.h.HeaderModel);
}
<div class="row" id="grid1">
@g_style.GetHtml(
tableStyle:"gridTable",
headerStyle: "gridHead",
footerStyle: "gridFooter",
rowStyle: "gridRow", alternatingRowStyle: "gridAltRow",
columns: g_style.Columns(
g_style.Column(header:"Xaxis", format:@item=>Html.DropDownList("value", (IEnumerable<SelectListItem>)Model.h.HeaderModel[0].Xaxis)),
g_style.Column(header: "Yaxis", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)Model.h.HeaderModel[0].Yaxis)),
g_style.Column(header: "Zaxis", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)Model.h.HeaderModel[0].Zaxis))
))
</div><input type="submit" value="Add new Trace" id="clickToAdd" />