Grid.MVC Single view for CRUD operations

I am using Grid.MVC(https://gridmvc.codeplex.com/) and I need to render a grid and CRUD for the grid in the same view.

@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));
        }

My controller

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

This works fine, but when I need to do an insert in that same view, I am unable to use the same model since it is an IEnumerable type.

 --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 })

My 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; }
}

Is there any way to use the same view for both the Grid.MVC and insert operations

Answers


make the list of Catagory (btw it's spelled 'Category') be a property of a new view model.

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

View:

 @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 })

Need Your Help

Dont display varaibile if it contains no value (PHP|

php soap

I am using SOAP to get some data from other service, i get XML and convert and display it with PHP. The thing is sometimes there is no value for it in XML file and i dont know how to display some m...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.