TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
albert albert
NA
524
0
Saving data with mvc 3
May 29 2012 7:16 AM
Hi everybody,
I have this:
Model:
[code]
public class Hour
{
public int HourID { get; set; }
public int HoursPerDay { get; set; }
//private DateTime monday;
[DataType(DataType.Date)]
public DateTime Monday { get; set; }
public int Days { get; set; }
public Hour(/*DateTime Monday*/)
{
Days = DateTime.Now.DayOfWeek - DayOfWeek.Monday;
Monday = DateTime.Now.AddDays(-Days);
}
}
[/code]
class week:
[code]
public class Week
{
public int WeekID { get; set; }
//private DateTime year;
//[DisplayFormat(DataFormatString = "{0:yyyy}",ApplyFormatInEditMode = true)]
//public DateTime Date { get; set; }
public string Name { get; set; }
[DataType(DataType.Date)]
public int? WeekNumber { get; set; }
public virtual IEnumerable<Hour> Hour { get; set; }
//DateTime date1 = new DateTime(2011, 1, 1);
//public t MyProperty { get; set; }
TimeSheetContext _context = new TimeSheetContext();
public int SelectID { get; set; }
public Week(/*int YearNumber*/)
{
YearNumber = DateTime.Now.Year;// .AddYears(0);
}
public DateTime WeekNumberss { get; set; }
//[DataType(DataType.Date)]
public int YearNumber { get; set; }
}
[/code]
the View:
[code]
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Hour</legend>
<table style="width: 100%;">
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.WeekID, "Week")
</div>
<div class="editor-field">
@Html.DropDownList("WeekID", String.Empty)
@Html.ValidationMessageFor(model => model.WeekID)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.Week.YearNumber, "jaar")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Week.YearNumber,Html.GetWeekOfYear())
@Html.ValidationMessageFor(model => model.Week.YearNumber)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.WeekNumber, "week")
</div>
<div class="editor-field">
@Html.DropDownList("weekId", (IEnumerable<SelectListItem>)Html.GetWeekNumbers(), new { onchange = "Selectedchange();" })
@Html.ValidationMessage("week")
</div>
</td>
<td>
<div class="editor-label">
@Html.Label("Maandag")
</div>
<div class="editor-field">
@Html.MyLabel("txtBox", Model.Monday.ToString(CultureInfo.CurrentCulture))
@Html.ValidationMessageFor(model => model.Monday)
</div>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<div class="editor-field">
</div>
</td>
</tr>
<tr>
</tr>
</table>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
[/code]
and the controller:
[code]
public class HourController : Controller
{
private TimeSheetContext db = new TimeSheetContext();
//
// GET: /Hour/
public ViewResult Index()
{
var hours = db.Hours.Include(h => h.Week);
return View(hours.ToList());
}
[HttpPost]
public ActionResult Index(int year)
{
return new EmptyResult();
}
/*
public ActionResult WikiDetails(bool editable)
{
//Wiki wiki = new Wiki
Wiki wiki = new Wiki
{
Name = "Hallo",
Url = "hallo.com",
Editable = editable
};
return View(wiki);
}
*/
//
// GET: /Hour/Details/5
public ViewResult Details(int id)
{
Hour hour = db.Hours.Find(id);
return View(hour);
}
//
// GET: /Hour/Create
public ActionResult Create()
{
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID");
return View(new Hour());
}
//
// POST: /Hour/Create
[HttpPost]
public ActionResult Create(Hour hour)
{
if (ModelState.IsValid)
{
db.Hours.Add(hour);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
//
// GET: /Hour/Edit/5
public ActionResult Edit(int id)
{
Hour hour = db.Hours.Find(id);
ModelState.AddModelError("Name", "What a shit name");
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
//
// POST: /Hour/Edit/5
[HttpPost]
public ActionResult Edit(Hour hour)
{
if (ModelState.IsValid)
{
db.Entry(hour).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
//
// GET: /Hour/Delete/5
public ActionResult Delete(int id)
{
Hour hour = db.Hours.Find(id);
return View(hour);
}
//
// POST: /Hour/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Hour hour = db.Hours.Find(id);
db.Hours.Remove(hour);
db.SaveChanges();
return RedirectToAction("Index");
}
/*
public DateTime getStartOfWeek(bool useSunday)
{
DateTime now = DateTime.Now;
int dayOfWeek = (int)now.DayOfWeek;
if (!useSunday)
dayOfWeek--;
if (dayOfWeek < 0)
{
dayOfWeek = 6;
}
return now.AddDays(-1 * (double)dayOfWeek);
}
*/
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
[/code]
and the extension method for the dropdownlist for week numbers:
[code]
public static IEnumerable<SelectListItem> GetWeekNumbers(this HtmlHelper helper /*Dictionary<int, DateTime> dayOfWeek*/ /*,DateTime allWeeks*/ /*int selectID*/)
{
var janFirst = new DateTime(DateTime.Today.Year, 1, 1);
//beware different cultures.
var firstWeek = janFirst.AddDays(1 - (int)(janFirst.DayOfWeek));
int j = -1;
foreach (var weekNumber in Enumerable.Range(0, 52).Select(i => new
{
weekStart = firstWeek.AddDays(i * 7),
//Tuesday = firstWeek.AddDays(i * 3)
}).TakeWhile(x => x.weekStart.Year <= janFirst.Year).Select(x => new
{
x.weekStart,
weekFinish = x.weekStart.AddDays(6)
}).SkipWhile(x => x.weekFinish < janFirst.AddDays(1)))
{
j++;
yield return new SelectListItem
{
Value = string.Join(",",new string[]
{
weekNumber.weekStart.ToShortDateString()
// weekNumber.weekStart.AddDays(1),
// weekNumber.weekStart.AddDays(2),
// weekNumber.weekStart.AddDays(3),
// weekNumber.weekStart.AddDays(4),
// weekNumber.weekStart.AddDays(5),
// weekNumber.weekStart.AddDays(6),
}),
Text = (j + 1).ToString(CultureInfo.InvariantCulture)
};
}
}//end method
[/code]
I 've also included the hole project.
but when I try to save the data I get this error:
Server Error in '/' Application.
An overflow occurred while converting to datetime.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:
System.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.
Source Error:
Line 80: { Line 81: db.Hours.Add(hour);
Line 82: db.SaveChanges();
Line 83: return RedirectToAction("Index"); Line 84: }
THX for helping..
Attachment:
manytomany26-05-2012workingversion.rar
Reply
Answers (
9
)
How to Upload Images In MVC4.0
Training on .NET Programming