尝试绑定网格时出现泛型错误

时间:2013-08-01 10:10:53

标签: c# asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid

我正在尝试将网格(Kendo UI)与用户在TextBox中输入的值绑定,但是当我启动程序时,我收到这样的错误,

  

异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [KendoPratapSampleMVCApp.Models.EmployeeDetails]',但此字典需要类型为“KendoPratapSampleMVCApp”的模型项.Models.ParentViewModel”。

当用户在TextBox中输入值然后按下提交按钮时,输入的值需要显示在网格中。

这是我的模特,

namespace KendoPratapSampleMVCApp.Models
{
    public class TextBoxGrid
    {
        public string EnteredValue { get; set; }
        public List<EmployeeDetails> employees;
    }   
    public class ParentViewModel
    {
        public EmployeeDetails EmployeeDetails { get; set; }
        public TextBoxGrid TextBoxGrid { get; set; }
    }
    public class EmployeeDetails
    {
        public string EmployeeId { get; set; }
        public string ManagerId { get; set; }
    }
}

这是我的控制器(我将用户输入的值绑定到网格)

namespace KendoPratapSampleMVCApp.Controllers
{
    public class EnterValuesGridController : Controller
    {     
        public ActionResult Index( TextBoxGrid model)
        {
            return View(GetEmployee());
        }
        [HttpPost]
        public ActionResult PostValues(TextBoxGrid model)
        {
            TempData["enteringValue"] = model.EnteredValue;
            return View(model);                
        }
        public  IEnumerable<EmployeeDetails> GetEmployee()
        {
            string enteredValueId =(string) TempData["enteringValue"];
            string managerId = "M" +enteredValueId;
            List<EmployeeDetails> empdtls = new List<EmployeeDetails>();
            EmployeeDetails em1 = new EmployeeDetails();
            em1.EmployeeId = enteredValueId;
            em1.ManagerId = managerId;
            empdtls.Add(em1);
            return empdtls;
        }
        public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
        {
            return Json(GetOrders().ToDataSourceResult(request));
        }
        private  IEnumerable<EmployeeDetails> GetOrders()
        {
            return GetEmployee();
        }
    }
}

这是我在显示网格的视图,

@model KendoPratapSampleMVCApp.Models.ParentViewModel
@{
    ViewBag.Title = "Index";
 }    
@using (Html.BeginForm("PostValues","EnterValuesGrid",FormMethod.Post))
{ 
    @Html.TextBoxFor(m=>m.TextBoxGrid.EnteredValue)    
    <input type="submit" name="Submitbutton1" value="Submit1" />    
     @(Html.Kendo().Grid<KendoPratapSampleMVCApp.Models.ParentViewModel>()    
    .Name("grid")
    .Columns(columns => {
        columns.Bound(s=>s.EmployeeDetails.EmployeeId).Filterable(false).Width(100);
        columns.Bound(s => s.EmployeeDetails.ManagerId).Filterable(false).Width(100);        
    })
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Read(read => read.Action("Orders_Read", "EnterValuesGrid"))
     )
  )        
}

有人请告诉我为什么会收到此错误。我怎么解决这个?

编辑:更改后评估方法

   [HttpPost]
    public ActionResult PostValues(TextBoxGrid model)
    {
        TempData["enteringValue"] = model.EnteredValue;
        var viewmodel = new ParentViewModel
        {
            TextBoxGrid = new TextBoxGrid { employees = GetEmployee().ToList() }

        };

        return View("Index", viewmodel);                       
    }

当我提交按钮时,它不显示网格中的值,但显示空格...

1 个答案:

答案 0 :(得分:2)

错误,因为您的视图模型是ParentViewModel,但是您给它

return view(GetEmployee);

IEnumerable,因此不是视图模型的对应对象。 我建议你按照以下方式进行操作

 public ActionResult Index( TextBoxGrid model)
    {
        var viewModel = new ParentViewModel
        {
            TextBoxGrid = new TextBoxGrid { employees = GetEmployee().ToList()}
            //but first change TextBoxGrid Property from emplyees to employees{get;set;}, second from  return empdtls; to  return empdtls.AsEnumarable();
        }

        return View(viewModel);
    }

你必须创建PostValues视图,或者将PostValues更改为Index,如果它给你的公共关系改变你的代码如下

    [HttpPost]
    public ActionResult PostValues(TextBoxGrid model)
    {
        TempData["enteringValue"] = model.EnteredValue;
        return View("Index",model);                
    }