0

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

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

当用户输入值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);                       
    }

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

4

1 回答 1

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 更改为索引,如果它给您重复更改您的代码如下

    [HttpPost]
    public ActionResult PostValues(TextBoxGrid model)
    {
        TempData["enteringValue"] = model.EnteredValue;
        return View("Index",model);                
    }
于 2013-08-01T10:26:53.257 回答