Controller getting blank model? White belt question.

2 posts, 1 answers
  1. Derek
    Derek avatar
    2 posts
    Member since:
    Apr 2013

    Posted 23 Apr 2013 Link to this post

    Hello,

    Spent hours trying to get this simple update sample to work.  Firefox shows that my model is being sent to my controller on an update but my controller receives a null model.  I must be missing something basic.  Thank you in advance.   VS2012.

    Firefox debug info:

    Parametersapplication/x-www-form-urlencoded

    models
    [{"OnTimeID":123,"Description":"Altamed staging issue","Status":"closed"}]

    View:

    <!doctype html>
    <html>
    @{
        ViewBag.Title = "OnTime Sample";
    }
    <head>
        <title>Kendo UI Web</title>
        <link href="~/styles/kendo.common.min.css" rel="stylesheet" />
        <link href="~/styles/kendo.default.min.css" rel="stylesheet" />
        <script src="~/js/jquery.min.js"></script>
        <script src="~/js/kendo.web.min.js"></script>
    </head>
    <body>
      
        <div class="form">
            <div class="selector">
                Edit Product:
                <input type="text" id="onTimeIDs" />
            </div>
            <div id="container" class="container">
                <ul>
                    <li>
                        <label>
                            OnTime ID</label>
                        <span data-bind="text:OnTimeID"></span></li>
                    <li>
                        <label>
                            Description</label>
                        <input type="text" class="k-textbox" data-bind="value: Description" /></li>
                    <li>
                        <label>
                            Status</label>
                        <input type="text" class="k-textbox" data-bind="value: Status" /></li>
                </ul>
                <button id="save">
                    Save</button>
            </div>
        </div>
        <script>
      
      
        var crudServiceBaseUrl = "/Services",
            dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: crudServiceBaseUrl + "/OnTimeIDs_Get",
                        dataType: "json"
                    },
                    update: {
                        url: crudServiceBaseUrl + "/OnTimeIDs_Update",
                        type: "POST",
                        dataType: "json"
                    },
                      
                    parameterMap: function (options, operation) {
                        if (operation !== "read" && options.models) {
                            return {                         
                                models: kendo.stringify(options.models)
                            };
                        }
                    }
                     
                },
      
      
                batch: true,
                schema: {
                    model: {
                        id: "OnTimeID",
                        fields: {
                            OnTimeID: {
                                editable: false,
                                nullable: true
                            },
                            Description: { type: "string" },
                            Status: { type: "string" }
                        }
                    }
                }
            }
        );
      
      
            $("#onTimeIDs").kendoDropDownList({
                dataSource: dataSource,
                dataTextField: "Description",
                dataValueField: "OnTimeID",
                change: function () {
                    var model = dataSource.get(this.value());
                    kendo.bind($("#container"), model);
                }
            }).data("kendoDropDownList")
                .one("dataBound", function () {
                    kendo.bind($("#container"), dataSource.view()[0]);
                });
      
            $("button").click(function () {
                dataSource.sync();
            });
        </script>
    </body>
    </html>


    Controller:

    Why does OnTimeIDs_Update() receive a null parameter?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MVCTimeCard.Models;
      
    namespace MVCTimeCard.Controllers
    {
        public class ServicesController : Controller
        {
      
            public ActionResult OnTimeIDs_Get()
            {
                List<OnTimeItem> otiList = new List<OnTimeItem>() {
                    new OnTimeItem {OnTimeID = 123, Description = "Altamed staging issue", Status = "Open"},
                    new OnTimeItem {OnTimeID = 456, Description = "HILL staging issue", Status = "Open"},
                    new OnTimeItem {OnTimeID = 789, Description = "PPMSI staging issue", Status = "Open"}
                };
      
                return Json(otiList, JsonRequestBehavior.AllowGet);         
            }
      
            [HttpPost]
            public ActionResult OnTimeIDs_Update( IEnumerable< OnTimeItem>  otis)
            {
      
                return Json(otis, JsonRequestBehavior.AllowGet);    
            }
        }
    }
  2. Answer
    Derek
    Derek avatar
    2 posts
    Member since:
    Apr 2013

    Posted 24 Apr 2013 Link to this post



    Okay, solved my own problem with the following change to my controller.

    public ActionResult OnTimeIDs_Update()
    {
        string models = Request["models"];
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<OnTimeItem> otis = serializer.Deserialize<List<OnTimeItem>>(models);
     
        return Json(otis); 
    }
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top