我是MVC的新手。我想在用户点击时动态创建文本框。
<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;
}
}
else {
alert("Cannot add another row.");
}
}
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);
}
}
和剃刀
<table class="table" id="AddSchedule">
<tr>
<td><input type="checkbox" name="chk[]" class="checkbox_style" /></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>
添加和删除文本框是可以的。但我不知道如何在数据库中保存数据。这是我的控制器
public ActionResult Create(EmployeeViewModel employeeViewModel)
{
if (ModelState.IsValid)
{
var emp = db.Employee.Create();
emp.ID = employeeViewModel.ID;
emp.LastName = employeeViewModel.LastName;
emp.FirstName = employeeViewModel.FirstName;
db.Employee.Add(emp);
var sched = db.FacultySchedule.Create();
sched.Schedule = employeeViewModel.Schedule;
sched.Room = employeeViewModel.Room;
sched.Subject = employeeViewModel.Subject;
db.FacultySchedule.Add(sched);
db.SaveChanges();
}
我尝试使用foreach但它仍然只保存第一个值,所以我将其删除了...我想我在标记中遗漏了一些东西......比如添加[]
来制作它像PHP一样的数组。而且我不知道如何在控制器中修复它。
如何修复控制器中的标记和循环以进行保存?感谢。
修改 这是viewmodel
public class EmployeeViewModel
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Schedule { get; set; }
public string Room { get; set; }
public string Subject { get; set; }
public virtual IEnumerable<FacultySchedule> FacultySchedule { get; set; }
}
这些是域模型
public partial class Employee
{
public Employee()
{
FacultySchedule = new HashSet<FacultySchedule>();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<FacultySchedule> FacultySchedule { get; set; }
}
public partial class FacultySchedule
{
public FacultySchedule()
{
Substitution = new HashSet<Substitution>();
}
public int ID { get; set; }
public int EmployeeID { get; set; }
public string Schedule { get; set; }
public string Room { get; set; }
public string Subject { get; set; }
public virtual Employee Employee { get; set; }
}