Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3671040
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:26:18+00:00 2026-05-19T02:26:18+00:00

I really didn’t know what title to give this question, but I’ll explain here:

  • 0

I really didn’t know what title to give this question, but I’ll explain here:

I have a View with a bunch of input fields in a table. Each row in the table represents a task, and each column a weekday, and the input fields in each cell are there to let the user input hours worked for that task and day.

I then have a submit button to post the hours when the user wants to save. But here’s the problem: Each timesegment (as the object that holds hours is called) also has the property Description, to let the user write a description of what has been done for a particular time segment reported.

So how could I get the description property for the selected timesegment input field and show it in another “description” input field, and then let the user modify the description and save it with the timesegment?

Here’s what I’ve done so far:

Action method to get the description:

    public ActionResult GetDescription(string name, int number, int year)
    {
        try
        {
            int taskId = Int32.Parse(name.SubstringAfter("Tasks[").Substring(0, 1));
            int timeSegmentId = Int32.Parse(name.SubstringAfter("CurrentTimeSegments[").Substring(0, 1));
            List<Task> tasks = _repository.GetCurrentTasks(number, year);
            var description = tasks[taskId].CurrentTimeSegments[timeSegmentId].Description;

            return Content(description);
        }
        catch (Exception)
        {

            return Content("");
        }
    }

jQuery:

    function getDescription() {
        $('.hourInput').focus(function () {
            var name = $(this).attr('name');
            var number = '<%: Model.WeekNumber %>';
            var year = '<%: Model.Year %>';
            var url = '<%=Url.Action("GetDescription", "Timesheet") %>';
            $.get(url, { name: name, number: number, year: year }, function (data) {
                $('#description').val(data);
            });
        });
    }

Now, as you can see, I have to parse the name attribute of the input field to get the object I’m after, and this seems like a bit of a hack… But it’s the only way I can see to get this information. So my question is, is there another cleaner way to do this?

UPDATE:

Here’s the part that creates the input fields in a nested for loop (looping through each task, and then for each task all its timesegments):

<% for (int i = 0; i < Model.Tasks.Count; i++)
           {
               var task = Model.Tasks[i];
        %>
        <tr class="taskrow">
            <td>
                <input type="button" value="Delete" id="<%:i %>" class="deletebutton" />
            </td>
            <td class="customer">
                <%: task.Project.Customer.Name %>
            </td>
            <td class="project">
                <%: task.Project.Name %>
            </td>
            <td class="task">
                <%: task.Name %>
            </td>
            <% for (int j = 0; j < task.CurrentTimeSegments.Count; j++)
               { %>
            <td>
                <%: Html.TextBoxFor(model => model.Tasks[i].CurrentTimeSegments[j].TimeSpanHours, new { @class = "hourInput" })%>
                <%: Html.ValidationMessageFor(model => model.Tasks[i].CurrentTimeSegments[j].TimeSpanHours)%>
            </td>
            <% } %>
            <td class="hourSum"><%:task.WeekTaskHours %></td>
        </tr>
        <% } %>

Note that this code is in a partialview if it matters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:26:18+00:00Added an answer on May 19, 2026 at 2:26 am

    you could use the $.data jQuery function to save extra information in your this element of the getDescription method. You need to do that when you create this element. I don’t know how you do that and if it is possible for you in your current design.

    to save the information it would be:

    $(element).data('taskId', taskId);
    $(element).data('timeSegmentId', timeSegmentId);
    

    If you give the code where you create this element, I could help you.

    Then the getDescription method would be

    $('.hourInput').focus(function () {
       var taskId = $(this).data('taskId');
       vat timeSegmentId = $(this).data('timeSegmentId');
       var number = '<%: Model.WeekNumber %>';
       var year = '<%: Model.Year %>';
       var url = '<%=Url.Action("GetDescription", "Timesheet") %>';
       $.get(url, { taskId : taskId, timeSegmentId: timeSegmentId, number: number, year: year }, function (data) {
          $('#description').val(data);
       });
    });
    

    and so in your controller

    public ActionResult GetDescription(string taskId, string timeSegmentId, int number, int year)
    {
       try
       {
          List<Task> tasks = _repository.GetCurrentTasks(number, year);
          var description = tasks[taskId].CurrentTimeSegments[timeSegmentId].Description;
    
          return Content(description);
       }
       catch (Exception)
       {
          return Content("");
       }
    }
    

    EDIT:

    According to how you create the input text boxes, I think you can do:

    <td>
       <%: Html.TextBoxFor(model => model.Tasks[i].CurrentTimeSegments[j].TimeSpanHours, new { @class = "hourInput", id = "uniqueId_" + i + j })%>
       <%: Html.ValidationMessageFor(model => model.Tasks[i].CurrentTimeSegments[j].TimeSpanHours)%>
       <script type="text/javascript">
          $(document).ready(function() {
             var selector = '#uniqueId_<%=i %><%=j %>';
             $(selector).data('taskId', <%=i %>);
             $(selector).data('timeSegmentId', <%=j %>);
          });
       </script>
    </td>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the vague title but I really didn't know what title to give
Sorry i really didn't know how to phrase the question any better but here
I really didn't know how to specify the problem in the title, so here's
I've found a similar question on stack overflow, but it didn't really answer the
I really didn't know how to call the topic, but I will try to
Ok I really didn't know how else to sum it up for the title.
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
I really didn't want to ask for help as I know I'll eventually figure
Well this is a really weird issue, I really didn't find anything on this
[Sorry for the title, I really didn't find a good one, if anyone has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.