从MVC表中获取选定的行id

本文关键字:id 获取 MVC | 更新日期: 2023-09-27 18:28:04

我是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为空我不知道如何获得选定的索引,请帮助我

从MVC表中获取选定的行id

如果您在所选行的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
}