将隐藏字段传递给控制器​​功能asp.net核心

时间:2019-03-11 00:09:25

标签: c# asp.net-core asp.net-core-mvc

我有以下活动,它是每个活动标题的活动的ICollection。我需要访问活动标头ID,以确保可以在此控制器操作中重新显示行。

public ActionResult ActivityLines_Read([DataSourceRequest]DataSourceRequest request ActivityLines activitylines ,int? activityID)
{            
        int ActivityLineId = actvitity.ActivityHeaderId;
        var result = GetAllActivityLines(ActivityLineId);
        var dsResult = result.ToDataSourceResult(request);
        return Json(dsResult);
}

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;
    }

要实现这一点,我在主窗体中做了一个隐藏字段以传递到编辑弹出窗口,这一切都很好,当我查看源代码时就可以了。

<div class="container py-5">
    <div class="row">
        <div class="col-md-10 mx-auto">
            <form>

                @Html.HiddenFor(x => x.ActivityHeaderId)
                <div class="form-group row">

                    <div class="col-sm-9">
                        <label for="inputFirstname">Activty Name</label>
                    </div>
                    @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })

                  </div>
                </div>
                <div class="form-group row">

                    <div class="col-sm-3">
                        <label for="inputLastname" class="form-control">Activity Start Date</label>
                        @Html.Kendo().DateTimePickerFor(model => model.ActivityDate)
                    </div>
                    <div class="col-sm-3">
                        <label for="inputLastname" class="form-control">Activity End Date</label>
                        @Html.Kendo().DateTimePickerFor(model => model.ActivityEndDate)
                    </div>

                </div>
                <div class="form-group row">

                    <div class="col-sm-3">
                        <label for="inputLastname" class="form-control">Location</label>
                        @foreach (var item in (SelectList)ViewBag.Location)
                        {
                            @Html.RadioButtonFor(model => model.OnSite, item.Value, false)
                            <label class="control-label">@item.Text</label>
                        }
                    </div>

                </div>
                <div class="form-group row">
                    <div class="col-md-10">
                        <label for="inputLastname" class="form-control">Description</label>
                        @Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 115, @rows = 10 })

                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-6">
                        <label for="inputCity">Status </label>
                        <select asp-for="Status"
                                class="form-control"
                                asp-items="@(new SelectList(@ViewBag.ProjectStatusTypes,"LookupCode", "LookupDescription"))"></select>


                    </div>
                    <div class="col-sm-3">
                        <label for="inputState">ActivityType </label>
                        <select asp-for="ActivityType"
                                class="form-control"
                                asp-items="@(new SelectList(@ViewBag.ProjectTypes,"LookupCode", "LookupDescription"))"></select>


                    </div>
                </div>

                <div class="form-group row">
                    <div class="col-sm-6">
                        <label for="inputCity">Staff </label>
                        <select asp-for="StaffId"
                                class="form-control"
                                asp-items="@(new SelectList(@ViewBag.ListOfStaff,"StaffID", "FirstName"))"></select>

                    </div>
                    <div class="col-sm-3">
                        <label for="inputState">Hours Left On Project </label>
                        <label for="inputState"><div class="badge" style="font-size:18px;">26</div> </label>
                        <label for="projecthours">If Porject hours fall below ten Contact Charlie.</label>
                    </div>
                </div>


                <div class="form-group row">

                    <div class="col-sm-12">

                        @(Html.Kendo().Grid<FuelActivityTrackerDal.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.Edit(); command.Destroy(); }).Width(450);


                                 })
                                        .ToolBar(toolbar => toolbar.Create())
                                                    .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ActvityEditorLines").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("ActivityLines_Update", "Activity").Type(HttpVerbs.Post)))

                        )
                    </div>
                </div>

                <div class="form-group row">
                    <div class="col-sm-6">

                    </div>
                </div>


                <button type="button" class="btn btn-primary px-4 float-right">Add Work Item</button>
                <button type="button" class="btn btn-primary px-4 float-right">Put Case & Client On Hold</button>
                <button type="button" class="btn btn-primary px-4">Cancel</button>
            </form>
        </div>
    </div>

我的主要问题是我如何通过

  

@ Html.HiddenFor(x => x.ActivityHeaderId)

对于第二个网格中的函数,它知道如何以及要带回哪些活动行,即活动1可以具有5个活动,我只需要无需将隐藏字段值作为参数发送给ActivityLines_Read即可。

请参见下图

enter image description here

编辑2

请注意,我不想将变量发布到该函数,因为使用它读取主类的行后,我该如何正确执行该操作,或者我使用模型的方式是否正确?

这是我在这里的模型,该模型以行作为集合。我应该只是在第二个网格中对集合进行裁判以确保数据正确吗?。

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 ActivityType { get; set; } //(int, null)

    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; }


}

0 个答案:

没有答案