我有一个表Employees
,里面有一堆列。我正在寻找一种方法来编辑这些列之一,以使其影响所有行。
该列称为accrualmonth
,用于sql server中的计算列。
。
理想情况下,我想提交一个表单,其下拉列表的数字从1到12。
我创建了一个CRUD操作,因此可以为单个行进行编辑,但理想情况下,我需要在“批量”中进行操作
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
ViewBag.AreaID = new SelectList(db.Areas, "AreaID", "Area1", employee.AreaID);
ViewBag.DisciplineID = new SelectList(db.Disciplines, "DisciplineID", "Discipline1", employee.DisciplineID);
ViewBag.HeadQuatersID = new SelectList(db.HeadQuaters, "HeadQuatersID", "Name", employee.HeadQuatersID);
ViewBag.ShiftID = new SelectList(db.Shifts, "ShiftID", "Shift1", employee.ShiftID);
ViewBag.SiteID = new SelectList(db.Sites, "SiteID", "SiteName", employee.SiteID);
return View(employee);
}
// POST: Accruals/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "EmployeeID,FullName,Email,StartDate,ShiftID,AreaID,DisciplineID,SiteID,ALCategory,HoursTaken,HoursCarriedForward,Comments,SickLeaveTaken,SickLeaveEntitlementRemaining,StudyLeaveEntitlement,StudyLeaveTaken,StudyLeaveRemaining,ExamLeaveTaken,ForceMajeure,BereavementLeaveTaken,MaternityLeaveTaken,ParentalLeaveTaken,AdoptionLeaveTaken,ManagerEmail,AreaManagerEmail,SickLeaveEntitlement,ActiveStatus,HeadQuatersID,Apprentice,AwardedLeave,TotalHoursThisYear,EntitlementRemainingThisYear,LeaveDate,LeavingDatEntitlement,accrualmonth,AllowanceToEndofMonth,TotalHolsToAccru")] Employee employee)
{
if (ModelState.IsValid)
{
db.Entry(employee).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AreaID = new SelectList(db.Areas, "AreaID", "Area1", employee.AreaID);
ViewBag.DisciplineID = new SelectList(db.Disciplines, "DisciplineID", "Discipline1", employee.DisciplineID);
ViewBag.HeadQuatersID = new SelectList(db.HeadQuaters, "HeadQuatersID", "Name", employee.HeadQuatersID);
ViewBag.ShiftID = new SelectList(db.Shifts, "ShiftID", "Shift1", employee.ShiftID);
ViewBag.SiteID = new SelectList(db.Sites, "SiteID", "SiteName", employee.SiteID);
return View(employee);
}
如何开始解决此问题?我是否尝试在编辑视图上使用foreach,以便它可以循环浏览所有条目。
查看:
model HolidayTracker.Models.Employee
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.EmployeeID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.FullName)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Email)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StartDate)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ShiftID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AreaID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.DisciplineID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.SiteID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ALCategory)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.HoursTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.HoursCarriedForward)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Comments)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.SickLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.SickLeaveEntitlementRemaining)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StudyLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StudyLeaveRemaining)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ExamLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ForceMajeure)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.BereavementLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.MaternityLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ParentalLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AdoptionLeaveTaken)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ManagerEmail)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AreaManagerEmail)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.SickLeaveEntitlement)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ActiveStatus)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.HeadQuatersID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Apprentice)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AwardedLeave)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.TotalHoursThisYear)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.EntitlementRemainingThisYear)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.LeaveDate)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.LeavingDatEntitlement)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.AllowanceToEndofMonth)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.TotalHolsToAccru)
<div class="form-group">
@Html.LabelFor(model => model.accrualmonth, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("accrualmonth", new[] { new SelectListItem() { Text = "1", Value = "1" },
new SelectListItem() { Text = "2", Value = "2" },
new SelectListItem() { Text = "3", Value = "3" },
new SelectListItem() { Text = "4", Value = "4" },
new SelectListItem() { Text = "5", Value = "5" },
new SelectListItem() { Text = "6", Value = "6" },
new SelectListItem() { Text = "7", Value = "7" },
new SelectListItem() { Text = "8", Value = "8" },
new SelectListItem() { Text = "9", Value = "9" },
new SelectListItem() { Text = "10", Value = "10" },
new SelectListItem() { Text = "11", Value = "11" },
new SelectListItem() { Text = "12", Value = "12" } },
htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.accrualmonth, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
根据要求:
答案 0 :(得分:0)
在您的视图中使用以下内容进行强类型定义,是的,请确保将所有控件替换为“ For”
{
List<SelectListItem> listOfMonths = new List<SelectListItem>();
listOfMonths.Add(new SelectListItem() { Text = "1", Value = "1" });
listOfMonths.Add(new SelectListItem() { Text = "2", Value = "2" });
listOfMonths.Add(new SelectListItem() { Text = "3", Value = "3" });
listOfMonths.Add(new SelectListItem() { Text = "4", Value = "4" });
listOfMonths.Add(new SelectListItem() { Text = "5", Value = "5" });
listOfMonths.Add(new SelectListItem() { Text = "6", Value = "6" });
listOfMonths.Add(new SelectListItem() { Text = "7", Value = "7" });
listOfMonths.Add(new SelectListItem() { Text = "8", Value = "8" });
listOfMonths.Add(new SelectListItem() { Text = "9", Value = "9" });
listOfMonths.Add(new SelectListItem() { Text = "10", Value = "10" });
listOfMonths.Add(new SelectListItem() { Text = "11", Value = "11" });
listOfMonths.Add(new SelectListItem() { Text = "12", Value = "12" });
}
@Html.DropDownListFor(model => model.accrualmonth, listOfMonths, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.accrualmonth, "", new { @class = "text-danger" })
如果您按照下面的步骤进行操作,将获得理想的结果。
使用任何名称创建一个partialView,并添加如下代码。
@model HolidayTracker.Models.Employee
List<SelectListItem> listOfMonths= (List<SelectListItem>)ViewData["listOfMonths"];
@using (Html.BeginCollectionItem("employees"))
{
@Html.HiddenFor(model => model.EmployeeID)
@Html.HiddenFor(model => model.FullName)
@Html.HiddenFor(model => model.Email)
@Html.HiddenFor(model => model.StartDate)
@Html.HiddenFor(model => model.ShiftID)
@Html.HiddenFor(model => model.AreaID)
@Html.HiddenFor(model => model.DisciplineID)
@Html.HiddenFor(model => model.SiteID)
@Html.HiddenFor(model => model.ALCategory)
@Html.HiddenFor(model => model.HoursTaken)
@Html.HiddenFor(model => model.HoursCarriedForward)
@Html.HiddenFor(model => model.Comments)
@Html.HiddenFor(model => model.SickLeaveTaken)
@Html.HiddenFor(model => model.SickLeaveEntitlementRemaining)
@Html.HiddenFor(model => model.StudyLeaveTaken)
@Html.HiddenFor(model => model.StudyLeaveRemaining)
@Html.HiddenFor(model => model.ExamLeaveTaken)
@Html.HiddenFor(model => model.ForceMajeure)
@Html.HiddenFor(model => model.BereavementLeaveTaken)
@Html.HiddenFor(model => model.MaternityLeaveTaken)
@Html.HiddenFor(model => model.ParentalLeaveTaken)
@Html.HiddenFor(model => model.AdoptionLeaveTaken)
@Html.HiddenFor(model => model.ManagerEmail)
@Html.HiddenFor(model => model.AreaManagerEmail)
@Html.HiddenFor(model => model.SickLeaveEntitlement)
@Html.HiddenFor(model => model.ActiveStatus)
@Html.HiddenFor(model => model.HeadQuatersID)
@Html.HiddenFor(model => model.Apprentice)
@Html.HiddenFor(model => model.AwardedLeave)
@Html.HiddenFor(model => model.TotalHoursThisYear)
@Html.HiddenFor(model => model.EntitlementRemainingThisYear)
@Html.HiddenFor(model => model.LeaveDate)
@Html.HiddenFor(model => model.LeavingDatEntitlement)
@Html.HiddenFor(model => model.AllowanceToEndofMonth)
@Html.HiddenFor(model => model.TotalHolsToAccru)
<div class="form-group">
@Html.LabelFor(model => model.accrualmonth, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.accrualmonth, listOfMonths, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.accrualmonth, "", new { @class = "text-danger" })
</div>
</div>
}
然后在上一页中使用此局部视图的位置添加了此代码,如果您说这是网格,则必须具有多行
在主视图中
List<SelectListItem> listOfMonths = new List<SelectListItem>();
listOfMonths.Add(new SelectListItem() { Text = "1", Value = "1" });
listOfMonths.Add(new SelectListItem() { Text = "2", Value = "2" });
listOfMonths.Add(new SelectListItem() { Text = "3", Value = "3" });
listOfMonths.Add(new SelectListItem() { Text = "4", Value = "4" });
listOfMonths.Add(new SelectListItem() { Text = "5", Value = "5" });
listOfMonths.Add(new SelectListItem() { Text = "6", Value = "6" });
listOfMonths.Add(new SelectListItem() { Text = "7", Value = "7" });
listOfMonths.Add(new SelectListItem() { Text = "8", Value = "8" });
listOfMonths.Add(new SelectListItem() { Text = "9", Value = "9" });
listOfMonths.Add(new SelectListItem() { Text = "10", Value = "10" });
listOfMonths.Add(new SelectListItem() { Text = "11", Value = "11" });
listOfMonths.Add(new SelectListItem() { Text = "12", Value = "12" });
foreach (var module in Model?.Related)
{
@{Html.RenderPartial("_YourPartialViewName", module, new ViewDataDictionary() { { "ListOfMonths", listOfMonths } });}
}
,控制器将收到这样的消息。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit( List<Employee > employees)
如果术前名称相同,则无需包含绑定,您可以通过此操作获得它