我是MVC的新手。想要使用单一View来应用简单的CRUD操作。我不想要单独的视图,如创建,编辑,删除,读取。我正在使用实体框架代码优先方法。
我已经尝试过以下代码。请帮我解决这个问题....
@model IEnumerable<WareHouse.WareHouseType>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid grid = new WebGrid(Model);
}
@using(Html.BeginForm) {
<div class="tdhdr1" >Warehouse Type Details
</div>
@grid.GetHtml( fillEmptyRows: true,
alternatingRowStyle: "alternative-row",
headerStyle: "tdhdr2",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Previous", nextText: "Next >",
lastText: "Last >>",columns: new [] {
grid.Column("WareHouseTypeID",header: "ID", canSort: false),
grid.Column("WareHouseTypeName",header:"Name ")
})
}
以上代码仅适用于gridview。 我想在同一个View上也有其他控件。是否可以使用Partial View? 我无法在同一视图上添加 Editorfor()。。
但是它会抛出错误**传入字典的模型项的类型是&#39; System.Collections.Generic.List`1 [WareHouse.WareHouseType]&#39;,但这个字典需要一个模型项键入&#39; WareHouse.WareHouseType&#39;。 **
部分查看代码
@model WareHouse.WareHouseType
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AddProduct</title>
</head>
<body>
<div>
<label>Id:</label>
@Html.TextBox("Id", Model.WareHouseTypeID)
<label>Name:</label>
@Html.TextBox("Name", Model.WareHouseTypeName)
</div>
</body>
</html>
一般观点代码:
@model IEnumerable<WareHouse.WareHouseType>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid grid = new WebGrid(Model);
}
@using(Html.BeginForm()) {
<div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
</div>
@Html.Partial("_WHT")
@grid.GetHtml( fillEmptyRows: true,
alternatingRowStyle: "alternative-row",
headerStyle: "tdhdr2",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Previous", nextText: "Next >",
lastText: "Last >>",columns: new [] {
grid.Column("WareHouseTypeID",header: "ID", canSort: false),
grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
})
}
控制器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WareHouse;
namespace CommonMasterUI.Controllers
{
public class WHTypeController : Controller
{
WareHouse.Context ctx = new WareHouse.Context();
IList<WareHouseType> list2 = new List<WareHouseType>();
[HttpGet]
public ActionResult Index()
{
IList<WareHouseType> wht = new List<WareHouseType>();
WareHouseRepository wr = new WareHouseRepository(ctx);
wht = wr.GetWareHouseTP().ToList();
for (int i = 0; i < wht.Count; i++)
{
WareHouseType wt = new WareHouseType();
wt.WareHouseTypeID = wht.ElementAt(i).WareHouseTypeID;
wt.WareHouseTypeName = wht.ElementAt(0).WareHouseTypeName.ToString();
list2.Add(wt);
}
return View(list2.ToList());
}
}
}