如何将所有列表记录从视图传递到控制器?

时间:2018-08-28 06:23:36

标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api razor

我正在尝试更新列表中的所有内容 我与View模型紧密结合在一起。 其中包括两个班级。

在显示列表时,类随内容一起提供,而其他为空。 这是导航属性     公共任务任务{get;组; }

我想更新TaskProcesser此类 我试图将其添加到列表中,但列表为空/ NULL

这是课程文件

public abstract class Tasks
    {
        public int Id { get; set; }
        public string Name { get; set; }        
        public string Remark { get; set; }
        public DateTime AddedAt { get; set; }
        public DateTime modedAt { get; set; }
    }
public class WorkTask : Tasks
    {
        public int Priority { get; set; }
        [Display(Name ="IsActive")]
        public bool IsCompleted { get; set; }


    }

    public class TaskProcesser
    {
        public int Id { get; set; }
        public bool IsCompleted { get; set; }
        public string Remark { get; set; }
        public DateTime ActivityDate { get; set; }

        public int TasksId { get; set; }
        public Tasks Tasks { get; set; }
    }

这是我绑定到视图的视图模型

public class TasksProcesserActivitesFromViewModel
    {   
        public WorkTask WorkTask { get; set; }      
        public TaskProcesser TaskProcesser { get; set; }
        public List<TasksProcesserActivitesFromViewModel> TasksProcesserActivitesFromViewModels { get; set; }
    }

这是控制器

public ActionResult Index()
        {            
            List<TasksProcesserActivitesFromViewModel> viewModel = new List<TasksProcesserActivitesFromViewModel>();
            foreach (var item in (from m in db.Tasks.OfType<WorkTask>() select m).ToList())
            {
                TasksProcesserActivitesFromViewModel var = new TasksProcesserActivitesFromViewModel()
                {
                    WorkTask = item,
                    TaskProcesser = new TaskProcesser()
                };
                viewModel.Add(var);
            }
            return View(viewModel);
        }



        [HttpPost]
        [ActionName("Index")]
        public ActionResult IndexPost(List<TasksProcesserActivitesFromViewModel> var)
        {
            //List<TaskProcesser> p = Request[]
            //foreach (var item in viewModel)
            //{

            //}    
            return View();
        }

这是视图

@model IEnumerable<SQLStudies.ViewModels.TasksProcesserActivitesFromViewModel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Index", "TaskProcesserActivities", FormMethod.Post))
{
<h2>Update work task Activities for today.</h2>
<hr />
<table class="table">
    <tr>
        <th>
            S/r No.
        </th>
        <th>
            @Html.DisplayNameFor(model => model.WorkTask.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.WorkTask.Priority)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.WorkTask.Remark)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaskProcesser.IsCompleted)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaskProcesser.Remark)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.HiddenFor(modelItem => item.WorkTask.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorkTask.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorkTask.Priority)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorkTask.Remark)
            </td>
            <td>
                @Html.EditorFor(modelItem => item.TaskProcesser.IsCompleted)
            </td>
            <td>
                @Html.EditorFor(modelItem => item.TaskProcesser.Remark)
            </td>
        </tr>
    }
</table>
<div>
    <input type="submit" value="submit" class="btn btn-primary" />
</div>

一切正常,但回发时我没有更新列表

谁能告诉我该怎么做?

0 个答案:

没有答案