0

嗨,我一直在获取需要在 kendo UI GRID 中显示的数据,最初我也能够看到按钮、文本框和网格,但是当我在文本框中输入值然后按下按钮时,我需要显示输入的内容剑道 UI GRID 中的值...

当我在谷歌浏览器中运行这个应用程序时,它给出了空网格,在输入值然后按下提交按钮但是当我在 IE8 中运行这个应用程序时,它在开始阶段本身就给出了这样的错误......

Unhandled exception at line 238, column 37 in Function code 0x800a138f - Microsoft JScript runtime error: 'data.EmployeeDetails.EmployeeId' is null or not an object

这是我的模型

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

这是我的控制器

public class EnterValuesGridController : Controller
{
    private static List<EmployeeDetails> empdtls;
    public ActionResult Index( ParentViewModel model)
    {
        var viewmodel = new ParentViewModel
        {
            TextBoxGrid = new TextBoxGrid { employees = GetEmployee().ToList() }
        };
        return View(viewmodel);
    }      
    [HttpPost]
    public ActionResult PostValues(TextBoxGrid model)
    {
        TempData["enteringValue"] = model.EnteredValue;
        var viewmodel = new ParentViewModel
        {
            TextBoxGrid = new TextBoxGrid { employees = GetEmployee().ToList() }
        };
        //ParentViewModel p = new ParentViewModel();
        //TextBoxGrid t = new TextBoxGrid();
        //t.EnteredValue = "a";
        //TempData["a1"] = t.EnteredValue;
        //t.employees = GetEmployee().ToList();
        //p.TextBoxGrid = t;
        //return View("Index", p);   
        return View("Index", viewmodel);        
    }
    public  IEnumerable<EmployeeDetails> GetEmployee()
    {
        string enteredValueId =(string) TempData["enteringValue"];
        string managerId = "M" +enteredValueId;
        empdtls = new List<EmployeeDetails>();
        EmployeeDetails em1 = new EmployeeDetails();
        em1.EmployeeId = enteredValueId;
        em1.ManagerId = managerId;
        empdtls.Add(em1);
        return empdtls.AsEnumerable();
    }
    public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
    {
        return Json(GetOrders().ToDataSourceResult(request));
    }
    private IEnumerable<EmployeeDetails> GetOrders()
    {
        return GetEmployee().AsEnumerable();
    }
}

这是我的观点

@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);        
    })
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource       
        .Ajax()
        .PageSize(20)
        .Read(read => read.Action("Orders_Read", "EnterValuesGrid"))
     )
  )        
} 

我不确定我在哪里做错了,请您对此提出任何想法.. 非常感谢....,我是否需要在 UI GRID 中进行任何更改我已经尝试更改 post values 方法...但是它对我不起作用...

更新: 更改 tempData 以查看 Bag ...

    [HttpPost]
    public ActionResult PostValues(ParentViewModel model)
    {
        ViewBag.item = model.TextBoxGrid.EnteredValue;

        var viewmodel = new ParentViewModel
        {
            TextBoxGrid = new TextBoxGrid { employees = GetEmployee().ToList() }

        };

        //ParentViewModel p = new ParentViewModel();
        //TextBoxGrid t = new TextBoxGrid();
        //t.EnteredValue = "a";
        //TempData["a1"] = t.EnteredValue;
        //t.employees = GetEmployee().ToList();
        //p.TextBoxGrid = t;
        //return View("Index", p);   
        return View("Index", viewmodel);        
    }

    public  IEnumerable<EmployeeDetails> GetEmployee()
    {
        string enteredValueId = (string)ViewBag.item;
        string managerId = "M" +enteredValueId;
        empdtls = new List<EmployeeDetails>();
        EmployeeDetails em1 = new EmployeeDetails();
        em1.EmployeeId = enteredValueId;
        em1.ManagerId = managerId;
        empdtls.Add(em1);
        return empdtls;
    }
4

2 回答 2

1

嗨,pratap,我只是更新您的代码,

模型

 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 EnteredValue { get; set; }
        public string EmployeeId { get; set; }
        public string ManagerId { get; set; }
    }

看法

@model TwoModelInSinglePageModel.EmployeeDetails
@{
    ViewBag.Title = "Index";
}
<script src="~/Script/Jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="~/Script/jquery-ui-1.8.20.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Script/kendo.all.min.js")" type="text/javascript"></script>
<script src="~/Script/kendo.web.min.js" type="text/javascript"></script>
<script src="~/Script/kendo.aspnetmvc.min.js" type="text/javascript"></script>

@using (Html.BeginForm("PostValues", "Test", FormMethod.Post))
{ 
    @Html.TextBoxFor(m => m.EnteredValue)    
    <input type="submit" name="Submitbutton1" value="Submit1" />               
    @(Html.Kendo().Grid<TwoModelInSinglePageModel.EmployeeDetails>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(s => s.EmployeeId);
        columns.Bound(s => s.ManagerId);
    })
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
           .Read(read => read.Action("Orders_Read", "Test"))
     )
  )        
}

控制器

private static List<EmployeeDetails> empdtls;
    public ActionResult PostValues()
    {
        return View();
    }

[HttpPost]
public ActionResult PostValues(EmployeeDetails model)
{
    ViewBag.item = model.EnteredValue;

    ParentViewModel viewmodels = new ParentViewModel
    {
        TextBoxGrid = new TextBoxGrid { employees = GetEmployee().ToList() }

    };

    ParentViewModel viewmodel = new ParentViewModel();
    EmployeeDetails em1 = new EmployeeDetails();
    for (int i = 0; i < viewmodels.TextBoxGrid.employees.Count(); i++)
    {

        em1.EmployeeId = viewmodels.TextBoxGrid.employees[i].EmployeeId;
        em1.ManagerId = viewmodels.TextBoxGrid.employees[i].ManagerId;
        viewmodel.EmployeeDetails = em1;
    }
    Session["EM1"] = em1;
    return View("PostValues", em1);
}

public List<EmployeeDetails> GetEmployee()
{
    string enteredValueId = (string)ViewBag.item;
    string managerId = "M" + enteredValueId;
    empdtls = new List<EmployeeDetails>();
    EmployeeDetails em1 = new EmployeeDetails();
    em1.EmployeeId = enteredValueId;
    em1.ManagerId = managerId;
    if (Session["EM1"] != null)
    {
        em1 = Session["EM1"] as EmployeeDetails;

        empdtls.Add(em1);
        Session["EM1"] = null;
    }
    else
    {
        empdtls.Add(em1);
    }
    return empdtls;
}

public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request, EmployeeDetails model)
{
    return Json(GetEmployee().ToDataSourceResult(request));
}
于 2013-08-05T06:47:47.523 回答
0

OK, I read through your post again and you want to submit the form when click on the button actually. So, let's try to put the value in the session

Session["enteringValue"] = model.EnteredValue;

And you can retrieve it using

string enteredValueId = (string)(Session["enteringValue"]);
于 2013-08-02T14:08:04.183 回答