将视图中的数据列表传递给控制器

时间:2015-03-14 07:21:22

标签: c# asp.net-mvc

我想将List<T>数据从视图传递给控制器​​。

我从控制器获取数据以这种方式查看:

@model IList<Test.Models.Incentive>
@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
  <input type="submit" value="Save" />
  <div class="tab-content"> 
    <table class="grid">      
      @foreach (var item in Model)
      {
        <tr>
          <td class="left">@Html.TextBoxFor(modelItem => item.From, new { @class = "wide" })</td>
          <td class="left">@Html.TextBoxFor(modelItem => item.To, new { @class = "wide" })</td>
          <td class="left">@Html.TextBoxFor(modelItem => item.IncentiveAmount, new { @class = "wide" })</td>
        </tr>
      }
    </table>
  </div>
}

我在网格中正确获取数据。 现在,当我更改/更新网格中的数据时,我希望将其保存回按钮单击时,我将其值设为null。

public class Incentive
{        
  public decimal From { get; set; }
  public decimal To { get; set; }
  public decimal IncentiveAmount { get; set; }  
}

public List<Incentive> GetIncentive()
{
  List<Incentive> obj = new List<Incentive>();
  obj.Add(new Incentive {From = 7m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 17m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 27m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 37m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 47m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 57m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 67m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 77m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 87m, To = 9.9m, IncentiveAmount = 25.0m });
  obj.Add(new Incentive { From = 97m, To = 9.9m, IncentiveAmount = 25.0m });
  return obj;         
}

// For Saving Data getting in controller
[HttpPost]
public ActionResult Save(IList<Incentive> item)
{
  try
  {
    // item is coming null ?????
  }
  catch (Exception ex)
  {
    throw ex;
  }
  return View();
}

如何获取此数据?

0 个答案:

没有答案