从表MVC中获取选定的行ID

时间:2014-10-27 17:54:31

标签: c# ajax asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我是MVC的新手,在AJAX帖子中获取表的选定索引时面临问题。

我有以下课程

 public class ContractModel
{
    public long HotelId { get; set; }
    public long RateCodeId { get; set; }
    public string RateCode { get; set; }
    public string HotelName { get; set; }
    public List<RateBandModel> RateBands { get; set; }
}

public class RateBandModel
{
    public long Id { get; set; }
    public DateBandModel Applicable { get; set; }
    public DateBandModel Bookable { get; set; }
    public string RoomType { get; set; }
    public long RoomTypeId { get; set; }
}

这是我的部分观点

@model Packages.Website.Models.ContractModel

@using (Ajax.BeginForm("SelectRateBand", "Contract", new AjaxOptions() { UpdateTargetId = "product-table" }))
{       
  <input type="hidden" id="rowId" name="rowId" />
  <table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
    <tr>
        <th class="table-header-repeat line-left"><a href="">Room Type</a>  </th>
        <th class="table-header-repeat line-left"><a href="">Board Basis</a></th>
        <th class="table-header-repeat line-left"><a href="">From Date</a></th>
        <th class="table-header-repeat line-left"><a href="">To Date</a></th>
        <th class="table-header-repeat line-left"><a href="">Book From</a></th>
        <th class="table-header-repeat line-left"><a href="">Book To</a></th>
        <th class="table-header-options line-left"><a href="">Options</a></th>
    </tr>
    @Html.HiddenFor(modelItem => Model.HotelId)
    @Html.HiddenFor(modelItem => Model.RateCodeId)
    @Html.HiddenFor(modelItem => Model.RateBands)
    @foreach (var item in Model.RateBands)
    {
        <tr>
            <td>
                @Html.HiddenFor(modelItem => item.Id)
                @Html.DisplayFor(modelItem => item.RoomType)
            </td>
            <td>@Html.DisplayFor(modelItem => item.BoardBasis)</td>
            <td>@Html.DisplayFor(modelItem => item.Applicable.FromDate)</td>
            <td>@Html.DisplayFor(modelItem => item.Applicable.ToDate) </td>
            <td>@Html.DisplayFor(modelItem => item.Bookable.FromDate) </td>
            <td>@Html.DisplayFor(modelItem => item.Bookable.ToDate) </td>
            <td class="options-width">
                <input type="submit"  name="command" class="form-submit" />

            </td>
        </tr>
    }
  </table>
}

在控制器

   [HttpPost]
    public ActionResult SelectRateBand(ContractModel contract)
    {
        //Some code here

    }

现在在控制器中我得到Contract.DateBands NULL 我不知道如何获得选择索引请帮助我

1 个答案:

答案 0 :(得分:3)

如果您在所选行的Id之后,可以使用Actionlink帮助程序将其作为路径值传递。我发现使用命名参数可以更多地传达你的目的。

@Html.ActionLink(linkText: "Edit",
                 actionName: "SelectRateBand", 
                 controllerName: "YourController", 
                 routeValues: new { id = item.Id }, 
                 htmlAttributes: new { @class = "icon-1 info-tooltip", @Title = "Edit" });

在控制器中,将操作更改为接受id参数以匹配从ActionLink发送的id。

[HttpPost]
public ActionResult SelectRateBand(string id)
{
    //Some code here

}