您好我是MVC的新手, 我想在视图中显示webgrid,我正在尝试在同一页面中显示创建和显示,我在webgrid中显示数据时遇到问题,
这是我的代码: 控制器:
[HttpPost]
public ActionResult Insert(Torder Model)
{
if(ModelState.IsValid)
{
try
{
ntity.Torders.Add(Model);
ntity.SaveChanges();
ModelState.Clear();
TempData["notice"] = "Successfully registered";
}
catch(Exception ex)
{
TempData["Failure"] = ex;
}
}
else
{
TempData["Failure"] = "Record Not Saved";
}
//var empoyees = Employee.GetList();
IEnumerable<Torder> model1 = GetProducts();
return View(model1);
}
public IEnumerable<Torder> GetProducts()
{
List<Torder> objStudent = new List<Torder>();
///*Create instance of entity model*/
/*Getting data from database for user validation*/
var _objuserdetail = (from data in ntity.Torders
select data);
foreach (var item in _objuserdetail)
{
objStudent.Add(new Torder { Cid = item.Cid, Ccustomername = item.Ccustomername, Citem = item.Citem, Corderamount = (int)item.Corderamount});
}
return objStudent;
}
答案 0 :(得分:0)
只需通过您的IEnumerable<Torder>
List<Torder>
页面就可以写foreach loop
并创建网格
--- Example:
public class Torder
{
public int Id {get;set;}
public string Name {get;set;}
}
[HttpPost]
public ActionResult Insert(Torder Model)
{
if(ModelState.IsValid)
{
try
{
ntity.Torders.Add(Model);
ntity.SaveChanges();
ModelState.Clear();
TempData["notice"] = "Successfully registered";
}
catch(Exception ex)
{
TempData["Failure"] = ex;
}
}
else
{
TempData["Failure"] = "Record Not Saved";
}
//var empoyees = Employee.GetList();
List<Torder> model1 = GetProducts();
return View(model1);
}
public List<Torder> GetProducts()
{
List<Torder> objStudent = new List<Torder>();
// your logic
return objStudent;
}
---------
Page:
-------------
//html code
@model List<Torder>
@foreach(Torder order in Model)
{
// here you can build you grid(table)
order.Name
order.Id
}
P.S将来我建议写&#34;清洁&#34;没有UI
的{{1}}(尝试了解Razor
- 它非常好Angular
) -
答案 1 :(得分:0)
很难从评论中的代码中分辨出来,但您可能只是错过了@grid.GetHtml()
您显示的块定义了网格,但现在您需要发出HTML,以便在页面上显示某些内容。把它放在关闭div标签之前的@ {}块之后。