This is a migrated thread and some comments may be shown as answers.

Model Data Not Present when passing from View to Controller

7 Answers 797 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 30 Sep 2014, 09:39 PM
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,

7 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 01 Oct 2014, 08:01 AM
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.

 
0
James
Top achievements
Rank 1
answered on 01 Oct 2014, 02:08 PM
Thanks for the reply.  That seems to do the trick.  So, Name() is not required for Kendo?
0
Atanas Korchev
Telerik team
answered on 01 Oct 2014, 03:02 PM
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.

 
0
Vinod
Top achievements
Rank 1
answered on 19 Jul 2017, 05:11 AM

html:

<h2>calendar event</h2>
<div id="scheduler"></div>

js:
<script>
$(function () {

            $("#scheduler").kendoScheduler({
                date: new Date(),
                height: 600,
                views: [
               "day",
               { type: "month", selected: true },
               "week",
               "month",
                ],
                timezone: "Etc/UTC",
                dataSource:{
                    transport: {
                        read:
                        {
                                url: "/Validation/GetCalendarEvents",
                                dataType: "json"
                                  },
                            update: {
                                url: "/Validation/UpdateCalendarEvent",
                                dataType: "json",
                                type:"POST"
                                
                            },
                            create: {
                                url: "/Validation/SaveCalendarEvent",
                                type: "POST",
                            },
                            destroy: {
                                url: "/Validation/DeleteCalendarEvent",
                                dataType: "json",
                                type: "POST",
                            },
                            parameterMap: function (options, operation) {
                                if (operation !== "read" && options.models) {
                                    alert(kendo.stringify(options.models));
                                    return { models: kendo.stringify(options.models) };
                                }
                            }
                          
                    },
                        batch:true,
                        schema: {
                            data: "data",
                            model: {
                                id: "taskId",
                                fields: {
                                    taskId: { from: "TaskID", type: "number" },
                                    title: { from: "title", defaultValue: "No title", validation: { required: true } },
                                    start: { type: "date", from: "startdate" },
                                    end: { type: "date", from: "enddate" },
                                    startTimezone: { from: "StartTimezone" },
                                    endTimezone: { from: "EndTimezone" },
                                    description: { from: "Description" },
                                    recurrenceId: { from: "RecurrenceID" },
                                    recurrenceRule: { from: "RecurrenceRule" },
                                    recurrenceException: { from: "RecurrenceException" },
                                    ownerId: { from: "OwnerID", defaultValue: 1 },
                                    isAllDay: { type: "boolean", from: "IsAllDay" }
                                }
                            }
                        },

                 }
          });
});

</script>

 

the above i have used to create scheduler event. I have created a table having name  'Events' 

sql scripts:

create table Events(
taskId int primary key,   
title  varchar(100),            
startdate date,
enddate date,   
startTimezone datetime,    
endTimezone  datetime,
description varchar(100),
recurrenceId  int,   
recurrenceRule varchar(100),
recurrenceException int,
isAllDay bit       
)
insert into Events values(1,'new','7/18/2017','7/18/2017',null,null,'newly',null,null,null,1)

I am able to fetch the data but when i am updating or creating it is not taking model to controller

Please help me out

 

Thanks in advance.

0
Bozhidar
Telerik team
answered on 21 Jul 2017, 05:33 AM
Hello Vinod,

I've opened a separate support ticket for your issues, since it's not relevant for the current thread. Let's continue our discussion there.

Regards,
Bozhidar
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
John
Top achievements
Rank 1
answered on 26 Feb 2019, 01:12 PM

If I comment out the .Name() parameter, then I cannot render the page:

Server Error in '/' Application.

Value cannot be null.
Parameter name: keyDescription: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: key

 

My problem is that the selected month/year isn't returned to the controller using ASP.NET MVC.

    <div class="col-sm-3 style=" margin-top: auto"">
        @(Html.Kendo().DatePicker()
            .Name("monthpicker")
            .Start(CalendarView.Year)
            .Depth(CalendarView.Year)
            .Format("MMMM yyyy")
            .Value(Model.StartDate)
            .DateInput()
            .HtmlAttributes(new { style = "width: 90%" })
        )
        @Html.ValidationMessageFor(model => model.StartMonth, "", new { @class = "text-danger" })
    </div>

 

        [HttpPost]
        public ActionResult SaveReportBuilder(ReportBuilderVM model)
        {
            //model shows null StartDate
...

}

StartMonth is defined as 

        [DataType(DataType.Date)]
        [Display(Name = "Start Month")]
        public DateTime? StartMonth { get; set; }

 

Controller initializes it to:

            rb.StartMonth = DateTime.Today;

Any help would be appreciated.

Thanks

0
John
Top achievements
Rank 1
answered on 27 Feb 2019, 08:38 PM

Found the solution:

        @(Html.Kendo().DatePickerFor(model => model.StartDate)
            .Start(CalendarView.Year)
            .Depth(CalendarView.Year)
            .Format("MMMM yyyy")
            .Value(DateTime.Today)
            .DateInput()
            .HtmlAttributes(new { style = "width: 90%" })

Tags
General Discussions
Asked by
James
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
James
Top achievements
Rank 1
Vinod
Top achievements
Rank 1
Bozhidar
Telerik team
John
Top achievements
Rank 1
Share this question
or