部分视图数据上的错误不会粘贴到模型上

时间:2015-03-07 04:35:45

标签: asp.net-mvc-5

美好的一天。我真的不知道我会修复哪一部分。 这是视图模型

    public class EmployeeViewModel
{
    public int ID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public virtual ICollection<FacultySchedViewModel> FacultyScheduleViewModels { get; set; }
}

public class FacultySchedViewModel
{
    public int ID { get; set; }
    public int EmployeeID { get; set; }
    public string Schedule { get; set; }
    public string Room { get; set; }   
}

主视图 - 在这里我将隐藏或显示它,因为它是可选的。

@Html.Partial("_Create", new LeavesAndTrainings.ViewModels.FacultySchedViewModel())

并在主视图中使用javascript在局部视图中动态创建文本框

<script>
function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for (var i = 0; i < colCount; i++) {

        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        switch (newcell.childNodes[0].type) {
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "checkbox":
                newcell.childNodes[0].checked = false;
                break;
            case "select":
                newcell.childNodes[0].selectedIndex = 0;
                clear_attrib();
                break;
        }
    }
}

function deleteRow(tableID) {
    try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        for (var i = 0; i < rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if (null != chkbox && true == chkbox.checked) {
                if (rowCount <= 1) {
                    alert("Cannot delete all the rows.");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }
        }
    } catch (e) {
        alert(e);
    }
}

这是局部视图 - 使用BeginCollectionItem在文本框上动态创建索引

<table class="table" id="AddSchedule">
        @using (Html.BeginCollectionItem("FacultySchedViewModel"))
        {
            <tr>
                <td><input type="checkbox" name="chk[]" class="checkbox_style" /></td>
                <td>
                    @Html.HiddenFor(model => model.ID, new { htmlAttributes = new { @class = "form-control" } })
                </td>
                <td>
                    @Html.EditorFor(model => model.Schedule, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Schedule, "", new { @class = "text-danger" })
                </td>
                <td>
                    @Html.EditorFor(model => model.Room, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Room, "", new { @class = "text-danger" })
                </td>
                <td>
                    @Html.EditorFor(model => model.Subject, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Subject, "", new { @class = "text-danger" })
                </td>
            </tr>
        }
    </table>

    <div class="form-group">
        <div class="col-md-12">
            <input type="button" name="add" value="Add" class="btn btn-default" onclick="addRow('AddSchedule')">
            <input type="button" name="remove" value="Remove" class="btn btn-default" onclick="deleteRow('AddSchedule')">
        </div>
    </div>

和控制器

public ActionResult Create()
    {
        EmployeeViewModel employeeViewModel = new EmployeeViewModel();
        ICollection<FacultySchedViewModel> schedViewModel = new List<FacultySchedViewModel>();
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(EmployeeViewModel employeeViewModel, ICollection<FacultySchedViewModel> schedViewModel)
    {
        if (ModelState.IsValid)
        {
            var emp = db.Employee.Create();
            emp.ID = employeeViewModel.ID;
            emp.LastName = employeeViewModel.LastName;
            emp.FirstName = employeeViewModel.FirstName;
            db.Employee.Add(emp);

            if (schedViewModel != null)
            {
                var sched = db.FacultySchedule.Create();
                foreach (var aaa in schedViewModel)
                {
                    sched.EmployeeID = employeeViewModel.ID;
                    sched.Schedule = aaa.Schedule;
                    sched.Room = aaa.Room;
                    sched.Subject = aaa.Subject;
                } 
            }     

            db.SaveChanges();

            return RedirectToAction("Index");
        }

运行创建后,点击保存按钮,EmployeeViewModel employeeViewModel有效而非空,但ICollection<FacultySchedViewModel> schedViewModel null 。我试图只使用EmployeeViewModel employeeViewModel,但我不知道如何保存它。我以这种方式保存模型,因为有从其他表中提取的默认值和值。

0 个答案:

没有答案