如何将数据显示到文本框中

时间:2017-12-05 15:30:40

标签: c# asp.net-mvc

我的观点::

 foreach (TSM.Models.Tasks itm in lstTasks.Where(x => x.Project.ID == item.ID).ToList())
            {

 foreach (TSM.Models.TimeSheetData itemm in ViewBag.HoursData)
 {
 foreach (TSM.Models.Tasks itm in lstTasks.Where(x => x.Project.ID == item.ID).ToList())
                {


     <td><input class="txtTaskHours date@(Date.AddDays(i).ToString("MMddyyy"))" 
                style="width:50px;" value="@itemm.Hours" type="number" 
                id="@(itm.ID.ToString() + ':' + Date.AddDays(i).ToString("MMddyyy"))" />
     </td>
 }

我的控制器:

var query = (from ts in db.TimeSheets join tsd in db.TimesheetData on ts.ID  
             equals tsd.TimeSheet.ID where ts.StartDate == thisWeekStart 
             && ts.EndDate == thisWeekEnd select new{ tsd.Hours, tsd.Date, tsd.TaskID }).ToList();
var TMSSS = (string.Join(",", query.Select(x => x.ToString())));
ViewBag.HoursData = TMSSS;
return PartialView("~/Views/TimeSheet/_TimeSheet.cshtml", ViewBag); 

我想将数据显示到Textbox。要显示数据,我使用ViewBag使用foreach循环来显示Textbox中的小时数。

我的Textbox数据保存在json中。 这是我的json值:

"TaskHours" : [{
                 "ID":"f2886ec7-47d3-e711-9666-000c297c9306:12042017",
                 "Value":"25"
               }]

1 个答案:

答案 0 :(得分:0)

只需删除string.Join行:

var query = (from ts in db.TimeSheets join tsd in db.TimesheetData on ts.ID  
             equals tsd.TimeSheet.ID where ts.StartDate == thisWeekStart 
             && ts.EndDate == thisWeekEnd select new{ tsd.Hours, tsd.Date, tsd.TaskID }).ToList();
// var TMSSS = (string.Join(",", query.Select(x => x.ToString())));
// ViewBag.HoursData = TMSSS;
ViewBag.HoursData = query;
return PartialView("~/Views/TimeSheet/_TimeSheet.cshtml", ViewBag);

您的控制器会将string分配给ViewBag.HoursData,并且您的视图需要IEnumerable<TSM.Models.TimeSheetData>,这可能是您的query变量。

<强>更新

对不起,我的不好。没有看到您在LINQ中选择动态对象。您需要创建一个新的包装类,用于放置LINQ中的值。

public class MyWrap {
public int Hours {get;set;}
public DateTime Date {get;set;}
public int TaskId {get;set;}
}

然后修改LINQ:

&& ts.EndDate == thisWeekEnd select new MyWrap { Hours = tsdHours, Date = tsd.Date, TaskId = ts.TaskID}).ToList()

最后在你的视图中:

foreach (TSM.Models.Tasks itm in lstTasks.Where(x => x.Project.ID == item.ID).ToList())
            {

 foreach (TSM.Models.MyWrap itemm in ViewBag.HoursData)
 {