Model Data Not Present when passing from View to Controller

4 posts, 1 answers
  1. James
    James avatar
    37 posts
    Member since:
    Sep 2014

    Posted 30 Sep 2014 Link to this post

    I have a MVC application where I am passing a data model to the view and then when I click the submit button I want to pass the model back to the controller.  When I receive the model in the controller it contains no data.  I am using the "MaskedTextBoxFor()" for most fields and then a "DatePickerFor()".

    01.@model Keying.Models.Invoice
    02. 
    03.@using (Html.BeginForm("Shipment", "Home", FormMethod.Post, new
    04.    {
    05.        @class = "form-horizontal",
    06.        role = "form",
    07.        id = "submitForm",
    08.        myInvoice = Model
    09.    }))
    10.{
    11.                <label>
    12.                    Invoice Number
    13.                </label>
    14.                @(Html.TextBoxFor(model => model.InvoiceNumber)
    15.                )
    16. 
    17.                <label>
    18.                    Invoice Date
    19.                </label>
    20. 
    21.                @(Html.Kendo().DatePickerFor(model => model.ShipDate)
    22.                      .Name("ship_date")
    23.                      .Value(DateTime.Today)
    24.                )
    25. 
    26.                <label>
    27.                    Bill to Account
    28.                </label>
    29. 
    30.                @(Html.Kendo().MaskedTextBoxFor(model => model.BillAccount)
    31.                      .Name("bill_account")
    32.                )
    33.}
    1.public ActionResult Shipment(Invoice myInvoice, string button)
    2.{
    3.       var shipment = new Shipment();
    4.       // other processing
    5.       return View(shipment);
    6.}

    So with the code snippets above the Invoice Data model going to the view is populated with some data but when it returns to the controller it contains no data.

    Thanks,
  2. Answer
    Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 01 Oct 2014 Link to this post

    Hi James,

    Try not setting the Name of the kendo windgets as it will break ASP.NET MVC model binding:

    @(Html.Kendo().DatePickerFor(model => model.ShipDate)
              .Value(DateTime.Today)
    )


    Regards,
    Atanas Korchev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. James
    James avatar
    37 posts
    Member since:
    Sep 2014

    Posted 01 Oct 2014 in reply to Atanas Korchev Link to this post

    Thanks for the reply.  That seems to do the trick.  So, Name() is not required for Kendo?
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 01 Oct 2014 Link to this post

    Hi,

    When you use DatePickerFor (or *For) the Name() is set internally to the property name specified in the expression. So if you use Kendo().DatePicker() you need to set Name() but when you use Kendo().DatePickerFor() you don't as it is set automatically. More info is available here.

    Regards,
    Atanas Korchev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET MVC is VS 2017 Ready