Kendo MVC - DropDown in grid not binding to model on update

16 views Asked by At

I pretty much have this entire grid working, but I'm unable to figure out two things.

  1. How come my drop down list, when I change the value isn't making it to my model in my update
  2. How do I keep the selected value from the model as the default value when I chose to edit my record.

I started with these two solutions to try to get here: Telerik example Telerik Example 2

Any help is appreciated, I can understand that this new DropdownList isn't being bound with the model, but i'm not entirely sure how to connect them. In my database the ProspectStatus is an int but I am returning it as a string, the dropdownlist is all possible values. The grid is bound correctly, the dropdownlist is bound correctly, but I don't know how to get the new selected dropdownlistvalue to the ActionResult EditingInline_Update

Here is my index.cshtml

@{
    ViewBag.Title = "TC";
}
@using Kendo.Mvc.UI
<style>
    #grid {
        /* chop the grid's height by 45px */
        height: calc(100% - 45px);
    }

        #grid .k-grid-content {
            /* chop the grid content's height by 25px */
            height: calc(100%% - 25px) !important;
        }
</style>
<div id="grid" style="height:100%">
    @(Html.Kendo().Grid<CleanerWebsite.Models.ApartmentProspectViewModel>()
    .Name("Grid")
    .Pageable(pageable => pageable
      .Input(true)
      .Numeric(true)
      .Info(true)
      .PreviousNext(true)
      .Refresh(false)
      .PageSizes(true)
      .ButtonCount(20))
    .Columns(columns =>
    {
        columns.Bound(p => p.ApartmentId).Title("Id").Width(105).Visible(false);
        columns.Bound(p => p.PropertyTitle).Title("Title").Width(105);
        columns.Bound(p => p.PropertyAddress).Title("Address").Width(105);
        columns.Bound(p => p.PhoneNumber).Title("Phone#").Width(105);
        columns.Bound(p => p.ProspectStatus).Title("Status").Width(105);
        columns.Bound(p => p.ProspectStatusDate).Title("Status Date").Width(105);
        columns.Command(command => { command.Edit();}).Width(200);
        //columns.Bound(p => p.SourcePhoto).ClientTemplate("<img src='#:data.SourcePhoto'/>").Width(140).Title("Image");
    })
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable()
    .Sortable()
    .HtmlAttributes(new { style = "height:100%;" })
    .Filterable()
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("_Apartments", "Home"))
        .Model(model =>
        {
            model.Id(p => p.ApartmentId);
            model.Field(p => p.PropertyTitle).Editable(false);
            model.Field(p => p.PropertyAddress).Editable(false);
            model.Field(p => p.ProspectStatusDate).Editable(false);
        })
        .Update(update => update.Action("EditingInline_Update", "Home"))
        )
)
</div>

Here is my HomeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CleanerBLL;
using Cleaner.DAL;
using Cleaner.Services;
using Cleaner.Model;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System.Reflection;

namespace CleanerWebsite.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            //var indexModel = new Cleaner.Services.ViewApartmentProspectService().GetAll(0, 10000, out _);
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult EditingInline_Update([DataSourceRequest] DataSourceRequest request, CleanerWebsite.Models.ApartmentProspectViewModel apartment)
        {
            if (apartment != null && ModelState.IsValid)
            {
                var apartmentService = new ApartmentService();
                apartmentService.UpdatePhoneNumberByApartmentId(apartment.ApartmentId, apartment.PhoneNumber);
                apartmentService.UpdateProspectStatusByProspectId(apartment.ApartmentId, apartment.ProspectStatus);
            }

            return Json(new[] { apartment }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult ProspectStatusList([DataSourceRequest] DataSourceRequest request)
        {
            var models = new ViewProspectStatusListService().GetPaged(0, 10000, out _);
            return Json(models.AsEnumerable(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult _Apartments([DataSourceRequest] DataSourceRequest request)
        {
            //string whereClause = $"UserId = {Session[Library.UserId]}";
            var service = new ViewApartmentProspectService().GetAll(0, 10000, out _);
            var plist = new ViewProspectStatusListService().GetPaged(0, 10000, out _);
            List<CleanerWebsite.Models.ApartmentProspectViewModel> models = new List<Models.ApartmentProspectViewModel>();
            for (int i = 0; i < service.Count; i++)
            {
                models.Add(
                    new Models.ApartmentProspectViewModel()
                    {
                        ApartmentId = service[i].ApartmentId,
                        PropertyAddress = service[i].PropertyAddress,
                        PhoneNumber = service[i].PhoneNumber,
                        PropertyTitle = service[i].PropertyTitle,
                        ProspectStatusDate = service[i].ProspectStatusDate,
                        SourcePhoto = service[i].SourcePhoto,
                        ProspectStatus = service[i].Status

                    });
            }
            return Json(models.ToDataSourceResult(request));

        }
    }
}

Here is my ApartmentProspectViewModel

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Permissions;
using System.Web;
using Cleaner.Model;

namespace CleanerWebsite.Models
{
    public class ApartmentProspectViewModel
    {
        public int ApartmentId { get; set; }
        public string PropertyTitle { get; set; }
        public string PropertyAddress { get; set; }

        public string PhoneNumber { get; set; }
        public string SourcePhoto { get; set; }

        [UIHint("ProspectStatusEditor")]
        public string ProspectStatus { get; set; }
        public DateTime? ProspectStatusDate { get; set; }
    }
}

here is my ProspectStatusEditor.cshtml

@using Kendo.Mvc.UI
@model CleanerWebsite.Models.ApartmentProspectViewModel
    
@(
Html.Kendo().DropDownList()
    .Name("Type")
    .DataTextField("IdValue")
    .DataValueField("Id")
    .OptionLabel("Select Status...")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("ProspectStatusList", "Home");
        })
        .ServerFiltering(false); ;
    })
)

just found it @model CleanerWebsite.Models.ApartmentProspectViewModel should be @model CleanerWebsite.Models.ProspectStatusModel

0

There are 0 answers