pass table values from view to controller mvc

…衆ロ難τιáo~ 提交于 2019-12-12 02:52:58

问题


I am working on a project in dot Net + share-point, In there controller get sharepoint list record & create a list of list object. In view catch the object as one model and dilplay in a table.

Table view enter image description here

Index.cshtml

@using( Html.BeginForm() )
{
<table id = "timeTrackingView" border="1" >

    <tr>
        <th>Projects</th>
        @for (var date = @Model.Dates.AddDays(-(@Model.DateRange-1)); date <= @Model.Dates; date = date.AddDays(1))
        {
            <th >@date.Day/@date.Month/@date.Year</th>
        }

    </tr>
   @{ double[] totalForToday = new double[@Model.DateRange];}

      @for (int i = 0; i < @Model.TimeTrakings.Count; i++)
      {         
          //var projectName = @Model.TimeTrakings[i][0].ProjectName;       
        int index = 0;     
        <tr>
            <td>
                @Model.TimeTrakings[i][0].ProjectName
            </td>
            @for (int j = 0; j < Model.TimeTrakings[i].Count(); j++)
            {
                totalForToday[index] = totalForToday[index] + @Model.TimeTrakings[i][j].Hours;
                 var time = @Html.EditorFor(model => @Model.TimeTrakings[i][j]);
                <td>@time</td>
              @*@Html.EditorFor(model => @Model.TimeTrakings[i][j].Hours)*@ 

                index++;  
            }

        </tr>
    }

    <tr>
    <td>Total for day</td>
        @foreach(var tot in totalForToday)
        {
            <td>@tot</td>
        }

    </tr>    
</table>
<input type="submit" name="SubmitBtn"/>
}

I am trying to get table data into controller.. help me im my code only gettinh hours, need get project name date & hours together.

My controller.

[HttpPost]
        public ActionResult Index(TimeTracking timeTracking)
        {
            ////do the logic here 
            return View(this.ReceiveTimeTrackingData());
        }

I Used two mode-: Model1

 public class TimeTrackingDetails

        {
             public string ProjectName { get; set; }
            public DateTime Date { get; set; }
            public double Hours { get; set; }        
        }

Model 2-:

public class TimeTracking

    {
              public List<List<TimeTrackingDetails>> TimeTrakings { get; set; }
    }

回答1:


If you need project name you should create an input. Now in your code you have just plain text with this line:

@Model.TimeTrakings[i][0].ProjectName

Just add another line onder it:

@Model.TimeTrakings[i][0].ProjectName
// this line will create additional hidden input and on form post it will be sended to server.
@Html.HiddenFor(x => x.TimeTrakings[i][0].ProjectName) 


来源:https://stackoverflow.com/questions/36005258/pass-table-values-from-view-to-controller-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!