我的asp.net视图中存在问题。我有一个foreach循环,我不能把我的第一次迭代放到我的表格中。
这就是代码:
<div id="@item.Name" class="tab-pane fade in active">
<div class="row">
@{int i = 0;}
@foreach (var prod in item.Products.Where(p => p.Type_Id == item.Id && p.IsDeleted != true))
{
using (Html.BeginForm("AddOrderRow", "Orders", new { enctype = "multipart/form-data", @class = "form-inline" }))
{
<input type="hidden" value="@ViewBag.orderId" name="Order_Id" />
<input type="hidden" value="@prod.Id" name="ProductID" />
<input type="hidden" value="@prod.Price" name="Price" />
<div class="col-sm-3" style="padding-bottom:20px">
<div align="center">
<button style="background-color:transparent; border-color:transparent">
<img class="img-responsive" src='@VirtualPathUtility.ToAbsolute(String.Format("{0}/{1}", System.Configuration.ConfigurationManager.AppSettings["ProdUploadPath"], prod.Image))' style="max-height: 100px" data-toggle="tooltip" title="@prod.Name">
</button>
</div>
</div>
if (i % 4 == 3)
{
@:</div><div class="row">
}
i++;
}
}
</div>
</div>
这就是它的样子:
正如你所看到的,只有第一行的第一行才是我的形式。
答案 0 :(得分:0)
我的建议如果你想要保存列表,请使用保存列表而不是迭代表单
示例控制器在这里;当我学习mvc hahaha时的那个
public ActionResult Create(int? area, string now, string search, string message)
{
ViewBag.Area = new SelectList(CoreList.GetDropdown("UnitArea"), "DropdownId", "Value", area);
ViewBag.SelectedArea = area;
var newdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
IFormatProvider culture = new CultureInfo(CultureInfo.CurrentCulture.Name, true);
if (!string.IsNullOrWhiteSpace(now))
{
DateTime.TryParse(now, culture, DateTimeStyles.AssumeLocal, out newdate);
}
ViewBag.dateFilter = newdate.ToString(CultureInfo
.GetCultureInfo(CultureInfo.CurrentCulture.Name)
.DateTimeFormat.ShortDatePattern);
ViewBag.Search = search;
var attendances = (from p in db.MstUnitDriverPairings
join x in db.TrnAttendanceUnits
.Where(o => o.PresentDate.Value.Year == newdate.Year &&
o.PresentDate.Value.Month == newdate.Month &&
o.PresentDate.Value.Day == newdate.Day)
on p.UnitId equals x.UnitId into j1
from x in j1.DefaultIfEmpty()
join v in db.TrnAttendanceUnits.OrderByDescending(o => o.PresentDate).Where(o => o.ClockOut == null && o.PresentDate < newdate)
on p.UnitId equals v.UnitId into jd1
from v in jd1.DefaultIfEmpty()
join y in db.TrnAttendanceDrivers.Where(o => o.PresentDate == newdate)
on p.DriverId equals y.DriverId into j2
from y in j2.DefaultIfEmpty()
join z in db.TrnAttendanceDrivers.OrderByDescending(o => o.PresentDate).Where(o => o.ClockOut == null && o.PresentDate < newdate)
on p.DriverId equals z.DriverId into jd2
from z in jd2.DefaultIfEmpty()
where (area == null ? true : p.MstUnits.UnitArea == area)
&& (string.IsNullOrEmpty(search) ? true : p.MstDrivers.DriverName.ToLower().Contains(search.ToLower())
|| p.MstUnits.PoliceNo.ToLower().Contains(search.ToLower()))
&& p.MstUnits.UnitPrimary == true
select new Attendance
{
DriverId = p.DriverId,
DriverName = p.MstDrivers.DriverName,
DriverIn = y.ClockIn == null ? false : true,
DriverOut = y.ClockOut == null ? false : true,
DriverInDate = y.ClockIn,
DriverInDateBefore = z.ClockIn,
DriverOutDate = y.ClockOut,
DriverOutDateBefore = z.ClockOut,
UnitId = p.UnitId,
PoliceNumber = p.MstUnits.PoliceNo,
UnitIn = x.ClockIn == null? false : true,
UnitOut = x.ClockOut == null ? false : true,
UnitInDate = x.ClockIn,
UnitInDateBefore = v.ClockIn,
UnitOutDate = x.ClockOut,
UnitOutDateBefore = v.ClockOut
}).ToList();
return View(attendances);
}
[HttpPost]
public ActionResult AttendanceSave(List<Attendance> Models, string presentDate, int? area, string search)
{
string message = string.Empty;
var newdate = new DateTime();
IFormatProvider culture = new CultureInfo(CultureInfo.CurrentCulture.Name, true);
if (DateTime.TryParse(presentDate, culture, DateTimeStyles.AssumeLocal, out newdate))
{
foreach (var item in Models)
{
TrnAttendanceDriver trnAttendanceDriver = db.TrnAttendanceDrivers.FirstOrDefault(o => o.PresentDate == newdate && o.DriverId == item.DriverId);
TrnAttendanceUnit trnAttendanceUnit = db.TrnAttendanceUnits.FirstOrDefault(o => o.PresentDate == newdate && o.UnitId == item.UnitId);
if (trnAttendanceDriver != null)
{
if (item.DriverIn && item.DriverInDate != null) trnAttendanceDriver.ClockIn = item.DriverInDate;
if (item.DriverOut && item.DriverOutDate != null)
{
trnAttendanceDriver.ClockOut = item.DriverOutDate;
trnAttendanceDriver.Status = false;
}
}
else
{
if (item.DriverIn)
{
var newDriverAttendance = new TrnAttendanceDriver();
newDriverAttendance.DriverId = item.DriverId;
newDriverAttendance.ClockIn = item.DriverInDate;
newDriverAttendance.PresentDate = newdate;
newDriverAttendance.Status = true;
db.TrnAttendanceDrivers.Add(newDriverAttendance);
}
}
if (trnAttendanceUnit != null)
{
if (item.UnitIn && item.UnitInDate != null) trnAttendanceUnit.ClockIn = item.UnitInDate;
if (item.UnitOut && item.UnitOutDate != null)
{
trnAttendanceUnit.ClockOut = item.UnitOutDate;
trnAttendanceUnit.Status = false;
}
}
else
{
if (item.UnitIn)
{
var newUnitAttendance = new TrnAttendanceUnit();
newUnitAttendance.UnitId = item.UnitId;
newUnitAttendance.ClockIn = item.UnitInDate;
newUnitAttendance.PresentDate = newdate;
newUnitAttendance.Status = true;
db.TrnAttendanceUnits.Add(newUnitAttendance);
}
}
}
}
try
{
db.SaveChanges();
}
catch //(Exception ex)
{
throw;
}
return RedirectToAction("Index","Transaction/Attendance", new {area, search, message, now = presentDate });
}
这是模特
public class Attendance
{
public int DriverId { get; set; }
[Display(Name = "Name")]
public string DriverName { get; set; }
[Display(Name = "Driver In")]
public bool DriverIn { get; set; }
[DataType(DataType.DateTime)]
public DateTime? DriverInDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime? DriverOutDateBefore { get; set; }
[DataType(DataType.DateTime)]
public DateTime? DriverInDateBefore { get; set; }
[Display(Name = "Driver Out")]
public bool DriverOut { get; set; }
[DataType(DataType.DateTime)]
public DateTime? DriverOutDate { get; set; }
public int UnitId { get; set; }
[Display(Name = "Police Number")]
public string PoliceNumber { get; set; }
[Display(Name = "Unit In")]
public bool UnitIn { get; set; }
[DataType(DataType.DateTime)]
public DateTime? UnitInDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime? UnitOutDateBefore { get; set; }
[DataType(DataType.DateTime)]
public DateTime? UnitInDateBefore { get; set; }
[Display(Name = "Unit Out")]
public bool UnitOut { get; set; }
[DataType(DataType.DateTime)]
public DateTime? UnitOutDate { get; set; }
}
以及此处的观点
@model List<Solution.Web.Areas.Transaction.Models.Attendance>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<nav class="navbar navbar-white" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#nav2">
<span class="sr-only">Toggle</span>
<span class="glyphicon glyphicon-search"></span>
</button>
<a class="navbar-brand">Search</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="nav2">
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
@Html.DropDownList("Area", string.Empty)
</div>
<div class="form-group">
<input type="text" name="now" id="dateFilter" class="form-control datepicker" value="@ViewBag.dateFilter" />
</div>
<div class="form-group">
<input type="text" name="Search" class="form-control" value="@ViewBag.Search" placeholder="Search" />
</div>
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span>
</button>
</form>
</div>
<!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
@using (Html.BeginForm("AttendanceSave", "Attendance", FormMethod.Post))
{
<input type="hidden" name="presentDate" value="@ViewBag.dateFilter" />
<input type="hidden" name="area" value="@ViewBag.SelectedArea" />
<input type="hidden" name="search" value="@ViewBag.Search" />
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Attendance</h3>
</div>
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th colspan="3">
Driver
</th>
<th colspan="3">
Unit
</th>
</tr>
<tr>
<th>
Name
</th>
<th>
In
@Html.CheckBox("drivercheckInHead")
</th>
<th>
Out
@Html.CheckBox("drivercheckOutHead")
</th>
<th>
Police Number
</th>
<th>
In
@Html.CheckBox("unitcheckInHead")
</th>
<th>
Out
@Html.CheckBox("unitcheckOutHead")
</th>
</tr>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
@Html.Hidden("Models[" + i + "].DriverId", Model[i].DriverId)
@Html.Hidden("Models[" + i + "].UnitId", Model[i].UnitId)
<td>
@Html.DisplayFor(m => m[i].DriverName)
</td>
<td>
@if (Model[i].DriverIn && Model[i].DriverOut)
{
@Html.Hidden("Models[" + i + "].DriverIn", Model[i].DriverIn)
@Html.Hidden("Models[" + i + "].DriverInDate", Model[i].DriverInDate)
@Model[i].DriverInDate.Value.ToString("dd MMMM yyyy HH:mm")
}
else
{
if (Model[i].DriverOutDateBefore == null && Model[i].DriverInDateBefore != null)
{
<label class="label label-danger">
@Model[i].DriverInDateBefore.Value.ToString("dd MMMM yyyy HH:mm")
</label>
}
else
{
<div class="input-group input-group-sm">
<span class="input-group-addon">
@Html.CheckBox("Models[" + i + "].DriverIn", Model[i].DriverIn, new { @class = "drivercheckIn" })
</span>
@Html.TextBox("Models[" + i + "].DriverInDate", Model[i].DriverInDate, new { @class = "datetimepicker drivercheckInDate" })
</div>
}
}
</td>
<td>
@if (Model[i].DriverIn && Model[i].DriverOut)
{
@Html.Hidden("Models[" + i + "].DriverOut", Model[i].DriverOut)
@Html.Hidden("Models[" + i + "].DriverOutDate", Model[i].DriverOutDate)
@Model[i].DriverOutDate.Value.ToString("dd MMMM yyyy HH:mm")
}
else
{
if (Model[i].DriverIn)
{
<div class="input-group input-group-sm">
<span class="input-group-addon">
@Html.CheckBox("Models[" + i + "].DriverOut", Model[i].DriverOut, new { @class = "drivercheckOut" })
</span>
@Html.TextBox("Models[" + i + "].DriverOutDate", Model[i].DriverOutDate, new { @class = "datetimepicker drivercheckOutDate" })
</div>
}
else
{
<span class="label label-info">Need driver in</span>
}
}
</td>
<td>
@Html.DisplayFor(m => m[i].PoliceNumber)
</td>
<td>
@if (Model[i].UnitIn && Model[i].UnitOut)
{
@Html.Hidden("Models[" + i + "].UnitIn", Model[i].UnitIn)
@Html.Hidden("Models[" + i + "].UnitIn", Model[i].UnitInDate)
@Model[i].UnitInDate.Value.ToString("dd MMMM yyyy HH:mm")
}
else
{
if (Model[i].UnitOutDateBefore == null && Model[i].UnitInDateBefore != null)
{
<label class="label label-danger">
@Model[i].UnitInDateBefore.Value.ToString("dd MMMM yyyy HH:mm")
</label>
}
else
{
<div class="input-group input-group-sm">
<span class="input-group-addon">
@Html.CheckBox("Models[" + i + "].UnitIn", Model[i].UnitIn, new { @class = "unitcheckIn" })
</span>
@Html.TextBox("Models[" + i + "].UnitInDate", Model[i].UnitInDate, new { @class = "datetimepicker unitcheckInDate" })
</div>
}
}
</td>
<td>
@if (Model[i].UnitIn && Model[i].UnitOut)
{
@Html.Hidden("Models[" + i + "].UnitOut", Model[i].UnitOut)
@Html.Hidden("Models[" + i + "].UnitOutDate", Model[i].UnitOutDate)
@Model[i].UnitOutDate.Value.ToString("dd MMMM yyyy HH:mm")
}
else
{
if (Model[i].UnitIn)
{
<div class="input-group input-group-sm">
<span class="input-group-addon">
@Html.CheckBox("Models[" + i + "].UnitOut", Model[i].UnitOut, new { @class = "unitcheckOut" })
</span>
@Html.TextBox("Models[" + i + "].UnitOutDate", Model[i].UnitOutDate, new { @class = "datetimepicker unitcheckOutDate" })
</div>
}
else
{
<span class="label label-info">Need unit in</span>
}
}
</td>
</tr>
}
</table>
</div>
@if (Model.Count() > 0)
{
<div class="panel-footer">
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-ok"> Submit</span>
</button>
</div>
}
</div>
}
&#13;
希望这会有所帮助,对不起我的错误代码,呵呵呵