vishal mane
vishal mane

Reputation: 296

Get selected row id from table MVC

I am new to MVC and facing problem in getting selected index of table in AJAX post.

I have following class

 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; }
}

And here is my partial view

@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>
}

In Controller

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

    }

Now in controller I get Contract.DateBands NULL And I have no Idea how to get selected index please help me

Upvotes: 0

Views: 8597

Answers (1)

JB06
JB06

Reputation: 1931

If you are after the Id of the selected row, you can pass it as a route value with your Actionlink helper. I find using the named parameters convey your purpose more.

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

And in your controller, change the action to accept an id parameter to match the id sent from the ActionLink.

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

}

Upvotes: 3

Related Questions