我正在将模型从get视图传递到post视图,并且我的一些值正确。我需要的最重要的值是null。 ID和UserID…我尝试过[bind]并改变了许多不同的方法来尝试使它起作用,但到目前为止,在我的httppost中它仍然为null。进入我的视图的值是正确的。 这是我的代码。
//GET: /TimeSheet/UserEditTimeSheet/{uid}{mid}{did}
public async Task<ActionResult> UserEditTimeSheet(string uid,int mid)
{
ViewBag.ProjectsSelect = new SelectList(
context.Projects.ToList(), "ProjectId", "ProjectName");
var tmaster = await context.TimeSheetMaster.Where(m => m.TimeSheetMasterId
== mid && m.UserId == uid).FirstOrDefaultAsync();
var tdetail = await context.TimeSheetDetails.Where(d => d.TimeSheetMasterId
== tmaster.TimeSheetMasterId).FirstOrDefaultAsync();
var tproj = await context.Projects.Where(p => p.ProjectId
== tdetail.ProjectId).FirstOrDefaultAsync();
var timeSheet = (from t in context.TimeSheetMaster
join d in context.TimeSheetDetails
on t.TimeSheetMasterId equals d.TimeSheetMasterId
where d.TimeSheetMasterId == mid
&& d.TimeSheetId == tdetail.TimeSheetId
select new TimeSheetProjectsModel()
{
TimeMasterId = t.TimeSheetMasterId,
TimeDetailsId = d.TimeSheetId,
P1w1d1 = d.Sunday,
P1w1d2 = d.Monday,
P1w1d3 = d.Tuesday,
P1w1d4 = d.Wednesday,
P1w1d5 = d.Thursday,
P1w1d6 = d.Friday,
P1w1d7 = d.Saturday,
ProjTotal1 = (int)t.TotalHours,
DaysOfWeek1 = tproj.ProjectName,
ProjectId1 = tproj.ProjectId,
UserId = t.UserId,
ProjDesc1 = t.Comment
});
if (timeSheet == null)
return HttpNotFound();
return View(await timeSheet.FirstOrDefaultAsync());
}
// POST: /TimeSheet/UserEditTimeSheet/{timesheetprojectsmodel}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> UserEditTimeSheet(TimeSheetProjectsModel model)
{
try
{
if(model == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (ModelState.IsValid)
{
// Rewrite TimeSheetMaster
var master = await context.TimeSheetMaster.Where(t =>
t.TimeSheetMasterId == model.TimeMasterId).FirstOrDefaultAsync();
master.Comment = model.ProjDesc1;
var details = await context.TimeSheetDetails.Where(t =>
t.TimeSheetMasterId == model.TimeMasterId &&
t.TimeSheetId == model.TimeDetailsId).FirstOrDefaultAsync();
details.ProjectId = model.ProjectId1;
details.Sunday = model.P1w1d1;
details.Monday = model.P1w1d2;
details.Tuesday = model.P1w1d3;
details.Wednesday = model.P1w1d4;
details.Thursday = model.P1w1d5;
details.Friday = model.P1w1d6;
details.Saturday = model.P1w1d7;
details.Hours = model.ProjTotal1;
context.Entry(master).State = EntityState.Modified;
context.Entry(details).State = EntityState.Modified;
await context.SaveChangesAsync();
TempData["Success"] = "TimeSheet Edit Successful";
return RedirectToAction("UserTimeSheets", model.UserId);
}
TempData["Error"] = "TimeSheet Edit Failure";
return RedirectToAction("UserTimeSheets", model.UserId);
}
catch (Exception ex)
{
Console.Write(ex.Message);
TempData["ErrorMaster"] = "TimeSheet Create Was Unsuccessful";
return RedirectToAction("TimeSheetList", "TimeSheet");
}
}