Reputation: 5929
I have got a LINQ to SQL entity class with an nullable int property called sTime which I am using to record number of seconds scheduled in for a task. e.g. database field 120000
I tried adding a property to the Task class but how do I initially set this property when I am using a LINQ to SQL entity.
public TimeSpan ScheduledTimeSpan {get; private set;}
int seconds = sTime ?? 0;
TimeSpan ts = new TimeSpan(0, 0, 0, seconds, 0);
ScheduledTimeSpan = ts;
I would do this in my View
<td><%= Html.Encode(task.ScheduledTimeSpan.TotalSeconds.ToString("hh:mm:ss")) %></td>
With MVC I am unsure on how and where to set and get sTime correctly when I am using UpdateModel(task); in my task controller. I was thinking I need to create a TimeSpan inside the controller action when doing the Get but that does not seem right to me, also where I should be setting this property? Anyone who can help?
Upvotes: 0
Views: 3308
Reputation: 15890
Yep, that makes sense now. :-)
This is how I would approach it...
Partial class for your TaskClass
public partial class Task
{
public TimeSpan ScheduledTimeSpan
{
get
{
int seconds = sTime ?? 0;
return new TimeSpan(0, 0, seconds);
}
set
{
if (value != null)
sTime = (int)value.TotalSeconds;
}
}
}
Then change your view to have
<p>
<label for="ScheduledTimeSpan">Scheduled Time:</label>
<%= Html.TextBox("ScheduledTimeSpan", Model.Task.ScheduledTimeSpan.ToString("hh:mm:ss"))%>
<%= Html.ValidationMessage("ScheduledTimeSpan", "*") %>
</p>
That should work all fine and dandy assuming that the model binder has no problem binding a "hh:mm:ss" string to a timespan.
If it does have problems, let me know.
HTHs, Charles
Upvotes: 1
Reputation: 5929
This is the input textbox on my view
<p>
<label for="sTime">sTime:</label>
<%= Html.TextBox("sTime", Model.Task.sTime)%>
<%= Html.ValidationMessage("sTime", "*") %>
</p>
This is the code in my .dbml designer.cs behind.
[Column(Storage="_sTime", DbType="Int")]
public System.Nullable<int> sTime
{
get
{
return this._sTime;
}
set
{
if ((this._sTime != value))
{
this.OnsTimeChanging(value);
this.SendPropertyChanging();
this._sTime = value;
this.SendPropertyChanged("sTime");
this.OnsTimeChanged();
}
}
}
Here is the code from my task controller for the edit action.
//
// GET: /Tasks/Edit/5
public ActionResult Edit(int id)
{
Task task = taskRepo.GetTask(id);
return View(new TaskViewModel(task));
}
//
// POST: /Tasks/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
Task task = taskRepo.GetTask(id);
try
{
UpdateModel(task);
taskRepo.Save();
return RedirectToAction("Details", new { id = task.taskId });
}
catch
{
ModelState.AddRuleViolations(task.GetRuleViolations());
return View(new TaskViewModel(task));
}
}
sTime is an Integer (time in seconds) and it needs to be that when it goes into the database field.. but the interface for this value needs to be a string of HH:MM:SS.
Which is why I am trying to use a TimeSpan, however I dont know where or how to set this up.
Does that make it clearer at all?
Upvotes: 0