Grid.MVC CRUD操作的单一视图

时间:2015-01-22 14:01:19

标签: c# asp.net-mvc asp.net-mvc-5 grid.mvc

我正在使用Grid.MVC(https://gridmvc.codeplex.com/),我需要在同一视图中为网格渲染网格和CRUD。

@model IEnumerable<Grid.Models.Catagory>
...
...
  @Html.Grid(Model).Columns(columns =>
        {
            columns.Add()
            .Encoded(false)
            .Sanitized(false)
            .Titled("CatagoryName").RenderValueAs(o => RenderCatagoryName("CatagoryName", o.CatagoryName));

            columns.Add()
            .Encoded(false)
            .Sanitized(false)
            .Titled("Color").RenderValueAs(o => RenderCatagoryName("Color", o.Color));

            columns.Add()
            .Encoded(false)
            .Sanitized(false)
            .Titled("Print Template").RenderValueAs(o => RenderCatagoryName("Print Template", o.PrintTemplate));
        }

我的控制器

    public ActionResult Index()
    {           
        return View(clients);
    }

这很好用,但是当我需要在同一个视图中插入时,我无法使用相同的模型,因为它是IEnumerable类型。

 --Insert values--
 //couldn't use the model for insert directly since it is a list type
        @Html.LabelFor(m => m.)
        @Html.TextBoxFor(m => m., new { maxlength = 50 })

我的ViewModel

public class Catagory
{

    public int CatagoryID { get; set; }
    public string CatagoryName { get; set; }
    public string Color { get; set; }
    public string PrintTemplate { get; set; }
    public string Language { get; set; }
}

是否有任何方法可以为Grid.MVC和插入操作使用相同的视图

1 个答案:

答案 0 :(得分:1)

Catagory(btw拼写为“类别”)的列表设为新视图模型的属性。

public class MyViewModel
{
     public Category CategoryToInsert { get; set; }
     public IEnumerable<Category> CategoriesToShow { get; set; }
}

查看:

 @model MyViewModel
...
...

@Html.Grid(Model.Categories).Columns(columns =>
  ...

@* Insert form: *@
@Html.LabelFor(m => m.CategoryToInsert.CategoryName)
@Html.TextBoxFor(m => m.CategoryToInsert.CategoryName, new { maxlength = 50 })