views:

398

answers:

2

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?

A: 

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?

Pricey
+1  A: 

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

Charlino
Thanks for the reply. I have not tried this yet, I wrote the get for this property already but how the set worked was confusing me.I was thinking of taking the same approach as you have shown above, what I dont understand and need to look into is how this works with regards to updating the model.I can see the set is changing sTime but how this eventually just works is what i am unclear about.. can only guess at the moment.
Pricey
When the value from the ScheduledTimeSpan form input is posted back, the model binder will parse it into a TimeSpan and then set the models ScheduledTimeSpan property, which in turn will set the sTime property. So therefore it will update the model. Savvy?
Charlino
Sounds magical :) thanks Charlino
Pricey
NP Pricey, let me know if you have any problems :-)
Charlino