【问题标题】:getting generics error when try to bind the grid尝试绑定网格时出现泛型错误
【发布时间】:2013-08-01 10:10:52
【问题描述】:

我正在尝试将网格(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);                       
    }

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

【问题讨论】:

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


    【解决方案1】:

    错误,因为您的视图模型是 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 更改为索引,如果它给您重复更改您的代码如下

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

    【讨论】:

    • 我更新了我的答案。但这取决于您的逻辑您必须如何修改,所以我只能说,您提供视图的对象必须与视图模型相对应(如果您的操作是 ActionResult 或 ViewResult 等)
    • 我需要将 GetEmployee() 之类的方法返回到视图以进行绑定.. 但是如果我这样写,我该如何返回...
    • 非常感谢....半个百分点完成了另一个疑问,当我在文本框中输入值并按下提交按钮时,它给出了这个错误the view 'PostValues' or its master was not found or no view engine supports the searched locations 你能告诉我怎么能我纠正这个..请
    • 不客气,我再次更改我的答案。如果它对您有用,您能否将答案标记为已接受。 :)
    • 对不起,我收到此错误资源无法找到。(http 404)我需要从 postvalues 调用 getemployee ...方法
    猜你喜欢
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 2021-01-18
    • 2021-05-25
    • 2019-09-04
    • 2014-04-22
    相关资源
    最近更新 更多