1

Why I get partial view data = null in the action post of the controller of main view ?

My Model:

 public class WorkTimeRegulationViewModel
    {
        public Guid Id { get;  set; }
        public string Name { get;  set; }
        public byte NumberOfAvailableRotations { get;  set; }
        public IEnumerable<WorkTimeViewModel> AssignedWorkTimes { get; set; }
        public IEnumerable<AllocationViewModel> EnroledParties { get; set; }

    }

My Main view:

@model TimeAndAttendance.web.ViewModels.WorkTimeRegulationViewModel
@{
    ViewBag.Title = "WorkTimeRegulationForm";
    Layout = "~/Views/Shared/_LayoutAR.cshtml";
}

<h4>TEST</h4>

@using (Html.BeginForm("Save", "WorkTimeRegulation"))
{

   // IEnumerable<TimeAndAttendance.web.ViewModels.WorkTimeViewModel> WorkTimeViewModel = Model.
    <div class="form-group">
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Name)
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.NumberOfAvailableRotations)
        @Html.Label("")
    </div>

    @Html.Partial("_WorkTime",Model.AssignedWorkTimes.ToList())
    <div>

    </div>

    @Html.HiddenFor(m => m.Id)
    // @Html.AntiForgeryToken()
    <button type="submit" class="btn btn-primary">ٍSave</button>
}

My Partial View:

@model IEnumerable<TimeAndAttendance.web.ViewModels.WorkTimeViewModel>

@(Html.Kendo().Grid(Model)
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Name);
        columns.Bound(p => p.NumberOfWorkHours).Width(120);
        columns.Bound(p => p.NumberOfShortDays).Width(120);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(500);
    })
    .ToolBar(toolbar => toolbar.Create()

    .Editable(editable => editable.Mode(GridEditMode.InLine).ConfirmDelete("test").DisplayDeleteConfirmation("Test"))
    //   .HtmlAttributes(new { style = "height:550px;" })
    .Sortable()

    .DataSource(dataSource => dataSource
        .Ajax()
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.Id))
        .Create(update => update.Action("EditingInline_Create", "WorkTimeRegulation"))
        .Read(read => read.Action("EditingInline_Read", "WorkTimeRegulation"))
        .Update(update => update.Action("EditingInline_Update", "WorkTimeRegulation"))
        .Destroy(update => update.Action("EditingInline_Destroy", "WorkTimeRegulation"))
    )
)

My controller for the main view:

 public ActionResult Save(WorkTimeRegulationViewModel x)
        {
            x.AssignedWorkTimes = null; //Why 
            return View();
        }

Through firebug: The grid in edit mode :

<input class="k-textbox valid" id="Name" name="Name" data-bind="value:Name" aria-invalid="false">

After I use a prefix :

@Html.Partial("_WorkTime", Model.AssignedWorkTimes,
    new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "AssignedWorkTimes" } })

I get the following:

<input class="k-textbox" id="AssignedWorkTimes_Name" name="AssignedWorkTimes.Name" data-bind="value:AssignedWorkTimes.Name">

but the collection still null!

Note: I have no controller for the partial view.

I don't know why x.AssignedWorkTimes = null

Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • You have not shown your partial, but it cannot bind because your are not generating the correct name attributes for binding - which would need to be `name="AssignedWorkTimes[0].SomeProperty"` etc (note the prefix and the collection indexer - and compare that with what you currently generate). Do not use `@Html.Partial()` for this. Use an `EditorTemplate` (or a `for` loop). Refer [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) –  Sep 13 '18 at 10:43
  • @StephenMuecke : Thanks a edit my question and clarify my partial view, Could you take a look please. And how to use `EditorTemplate` instead of `@Html.Partial()` I want to render my partial view and pass a model to it? – Anyname Donotcare Sep 13 '18 at 11:18
  • 1
    I don't know `Kendo().Grid` or what name attributes its generating for your form controls - but they need to be in the format I noted above (and as per the link I gave you). Note that you can pas the prefix (i.e. `AssignedWorkTimes`) using the technique in [getting the values from a nested complex object that is passed to a partial view](https://stackoverflow.com/questions/29808573/getting-the-values-from-a-nested-complex-object-that-is-passed-to-a-partial-view/29809907#29809907) - but I do not know your `Grid()` method is generating the collection indexers –  Sep 13 '18 at 11:27
  • @StephenMuecke : I edit my question, Could you take a look please – Anyname Donotcare Sep 13 '18 at 12:55
  • 1
    Your partial does not generate form controls with collection indexers, and that would only bind to your model is it contained a property `public WorkTimeViewModel AssignedWorkTimes { get; set; }` (i.e. a single `WorkTimeViewModel`, not a collection. I don't use Kendo, so I am not sure what would need to be done to generate the correct `name` attributes for binding (but [this post](https://www.telerik.com/forums/binding-to-a-collection-property-of-a-view-model) and its links might help) –  Sep 13 '18 at 13:00

0 Answers0