MVC控制器始终获得空模型

时间:2019-03-09 22:40:03

标签: c# asp.net-core model-view-controller kendo-ui kendo-grid

我正在使用kendo ui网格,在使用id传递给该函数时遇到真正的麻烦,活动通过null传递,我必须先获取

我的主要问题是如何获得

<div class="form-group row">
  <div class="col-sm-12">                        
      @(Html.Kendo().Grid<ActivityTrackerDal.Models.ActivityLines>
                    ().Name("activityLines")
                     .Events(e => e.Edit("onEdit"))
                     .Columns(columns =>
                    {
                        columns.Bound(p => p.Description).Filterable(false);
                        columns.Bound(p => p.StartTime).Filterable(false);
                        columns.Bound(p => p.EndTime).Filterable(false);
                        columns.Bound(p => p.Status);
                        columns.Command(command => command.Custom("ViewDetails").Click("showDetails"));

                    })
                            .ToolBar(toolbar => toolbar.Create())
                                        .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ActivityEditor").Window(w => w.Title("Edit Provider").Width(500)))
                                    .Scrollable()
                                    .Pageable()
                                    .Sortable()
                                    .Filterable()
                                    .DataSource(dataSource => dataSource
                                    .Ajax()
                                            .Events(events => events.Error("error_handler"))
                                            .Model(model => model.Id(p => p.ActivityLineId))
                    .Events(events => events.Error("error_handler"))
                    .Read(read => read.Action("ActivityLines_Read", "Activity"))
                    .Update(update => update.Action("Activity_Update", "Activity").Type(HttpVerbs.Post)))

              )
 </div>
</div>

下面的“我的表单”弹出窗口

        

        <div id="details-container"> 
          <div class="form-group row">
                <div class="col-sm-9">
                    <label for="inputFirstname">Activty Name</label>
                    @Html.TextAreaFor(model => model.First().Name, new { @class = "whatever-class", @cols = 115, @rows = 10 })

                </div>
            </div>
            <div class="form-group row">
                <div class="col-md-10">
                    <label for="inputLastname" class="form-control">Description</label>


                </div>
            </div>
            <div class="form-group row">
                <div class="col-md-6">
                    <label for="inputLastname" class="form-control">Start Time</label>


                </div>
                <div class="col-md-6">
                    <label for="inputLastname" class="form-control">End Time </label>

                </div>
            </div>    
        </div>
        <input type="submit" class="btn btn-file px-4" value="Save Work Item" />


        <button type="button" class="btn btn-primary px-4">Cancel</button>

    </form>
</script>

这是我的两个带有主动线的控制器函数,它们总是会以null的形式出现,因此我必须再次进入数据库并获取它们,这似乎不正确,但是我不需要我如何将主动ID传递给那些函数。

这是我的模特第一位

public class ActivityHeader
{ 
    public int ActivityHeaderId { get; set; } //(int, null)
    public DateTime? ActivityDate { get; set; } //(date, null)
    public string Name { get; set; } //(nvarchar(350), null)
    public DateTime? ActivityEndDate { get; set; } //(datetime, null)
    public string ProblemDescription { get; set; }
    public string Description { get; set; } //(nvarchar(max), null)

    public int? ActivityLinesId { get; set; } //(int, null)
    public int? HoursLeftOnProject { get; set; } //(time(7), null)
    public int? Status { get; set; } //(nchar(10), null)
    public string  StatusName { get; set; }
    public DateTime? CreatedDate { get; set; } //(date, null)
    public string CreatedBy { get; set; } //(nvarchar(50), null)
    public bool? isActive { get; set; } //(bit, null)
    public bool? isDeleted { get; set; } //(bit, null)
    public bool? isArchived { get; set; } //(bit, null)
    public int? SOP { get; set; } //(nvarchar(50), null)
    public int? OnSite { get; set; }
    public int? Remote { get; set; }

    public int? DepartmentId { get; set; } //(int, null)
    public string EmployeeName { get; set; } //(nvarchar(301), null)
    [ForeignKey("StaffId")]
    public int? StaffId { get; set; }
    public  virtual StaffMembers StaffMembers { get; set; }

    public ICollection<ActivityLines> ActivityLines { get; set; }


}

public class ActivityLines
{
    [Key]
    public int ActivityLineId { get; set; } //(int, not null)
    public int? ActivityHeaderId { get; set; } //(int, null)
    public string Description { get; set; } //(nvarchar(max), null)
    public string Notes { get; set; } //(nvarchar(max), null)
    public DateTime? StartTime { get; set; } //(time(7), null)
    public DateTime? EndTime { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; } //(time(7), null)
    public int? StaffMemeber { get; set; } //(int, null)
    public bool? isActive { get; set; } //(bit, null)
    public bool? isDeleted { get; set; } //(bit, null)
    public string CreatedBy { get; set; } //(nvarchar(50), null)
    public DateTime? CreatedDate { get; set; } //(date, null)
    public int? Status { get; set; } //(int, null)

}
    [AcceptVerbs("Post")]
public ActionResult Activity_Update([DataSourceRequest]DataSourceRequest request, ActivityLines activity,int? activityId)
    {
        int ActivityLineId =(int) activityId;
        ActivityLines nLines = _activityRepo.GetActivityLineById(activityId);
        nLines.Description = nLines.Description + "test do i savE";
        _activityRepo.UpdateActivityLines(nLines);
        return Json(new[] { nLines }.ToDataSourceResult(request, ModelState));
    }

public ActionResult ActivityLines_Read([DataSourceRequest]DataSourceRequest request, int? activityID)
    {
        if (activityID == 0)
        {
            activityID = 1;
       }

        var result = GetAllActivityLines(1);

        var dsResult = result.ToDataSourceResult(request);
        return Json(dsResult);
    }

如上所示,我正在使用

  

var result = GetAllActivityLines(1);

这是正确的,它应该从任何形式的ID中获取ID,以确保某些人以前曾经这样做过。

用于showDetails函数的JavaScript

<script type="text/javascript">
var detailsTemplate = kendo.template($("#template").html());

   function showDetails(e) {
      e.preventDefault();

     var dataItem = this.dataItem($(e.currentTarget).closest("tr")); // dataItem contains the ActivityLineId
    var wnd = $("#Details").data("kendoWindow");

    wnd.content(detailsTemplate(dataItem));
    wnd.center().open();
}

function onEdit(e) {
    var model = e.model;
    var activityLinesGrid = e.container.find("#activityLines").data("kendoGrid");
    activityLinesGrid.dataSource.read({ activityID: model.ActivityID });
}

这是我用来将活动行放入网格中的功能,该功能确实可以按屏幕截图显示。

public List<ActivityLines> GetActivityLines(int? activityHeaderId)
{
        int? _activityHeaderId = activityHeaderId;
        var staffRepo = new StaffRepositry(_db);

        List<ActivityLines> _activityLines = new List<ActivityLines>();
        _activityLines = _db.ActivityLines.AsNoTracking().Where(a=>a.ActivityHeaderId== _activityHeaderId).ToList();
        return _activityLines;


    }

截屏以证明活动线正在获取数据。

编辑2

当返回到活动更新功能时,活动为空,但我的主要问题是如何将ID传递给弹出窗口,以便我可以保存和更新正确的模型,我认为应该将该模型传递回其中的数据,但不符合下面的屏幕截图。

enter image description here

enter image description here

我也正在使用视图模型作为它的一部分,但是当我去建模时。它没有显示各个字段。

public   class ActivityEditViewModal
{
    public  int ActivityHeaderId { get; set; } //(int, null)
    public DateTime? ActivityDate { get; set; } //(date, null)
    public string Name { get; set; } //(nvarchar(350), null)
    public DateTime? ActivityEndDate { get; set; } //(datetime, null)
    public string Description { get; set; } //(nvarchar(max), null)
    public int? ActivityLinesId { get; set; } //(int, null)
    public int? HoursLeftOnProject { get; set; } //(time(7), null)
    public int Status { get; set; } //(nchar(10), null)
    public int ActivityType { get; set; } //(int, null)
    public DateTime? CreatedDate { get; set; } //(date, null)
    public string CreatedBy { get; set; } //(nvarchar(50), null)
    public bool? isActive { get; set; } //(bit, null)
    public bool? isDeleted { get; set; } //(bit, null)
    public bool? isArchived { get; set; } //(bit, null)
    public int? DepartmentId { get; set; } //(int, null)
    public string EmployeeName { get; set; } //(nvarchar(301), null)
    public int? SOP { get; set; } //(nvarchar(50), null)
    public int? OnSite { get; set; }
    public int? Remote { get; set; }
    public string StatusName { get; set; }
    public string StaffName { get; set; }        
    public int StaffID { get; set; }
    public enum ActivityTypeEnum
    {
        None,
        Project = 1,
        Case = 2,
        Development

    }
    public ICollection<ActivityLines> ActivityLines { get; set; }
    private ActivityTypeEnum _activityTypeEnum;
    public ActivityTypeEnum CurrentDifficulty
    {
        get { return _activityTypeEnum; }
        set { _activityTypeEnum = value; }
    }
}

1 个答案:

答案 0 :(得分:1)

我觉得您错过了将模型ID的隐藏输入添加到自定义编辑器模板“ ActivityEditor”的过程。

 @Html.HiddenFor(model => model.ActivityLineId)