C# 使用Ajax.ActionLink获取所选行的ID

C# 使用Ajax.ActionLink获取所选行的ID,c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,我有一个视图,看起来像: @foreach (var car in Model) { <tr> <td> @car.ID </td> <td> @car.carMake <

我有一个视图,看起来像:

          @foreach (var car in Model)
            {
            <tr>
                <td>
                    @car.ID
                </td>
                <td>
                    @car.carMake
                </td>
                <td>
                    @car.carModel
                </td>
                <td>
                    <div id="result"></div> //update result based on ID

                </td>
                <td>
                 Check for @Ajax.ActionLink(
                               @car.carMake,
                               "getUpdate",
                               "Home",
                               new { ID = car.ID },
                               new AjaxOptions
                                {
                               UpdateTargetId = "result", //use car.ID here? not sure
                               InsertionMode = InsertionMode.Replace,
                               HttpMethod = "Get"
                                })                      
                </td>
            </tr>
            }
如果我单击BMW的操作链接,我只希望该行使用部分视图操作进行更新。我尝试使用JQuery时使用了:

ID = $('#car_ID').val(); on a @Html.HiddenFor(model => car.ID)
但它一直在获取第一辆车的ID。我不确定我将如何进行这项工作


我见过这个:但还没有起作用。

这个有效。请让我知道你的想法,如果还有什么你需要完成你的工作。 这是你怎么做的。这是您的局部视图(_PartialView.cshtml):

模型测试2016 1006.Controllers.CarModel
更新行:@Html.Raw(Model.CarId)、@Html.Raw(Model.CarMake)、@Html.Raw(Model.theCarModel)
这是您的控制器/型号:

 public class CarModel
{
    public int CarId { get; set; }
    public string CarMake { get; set; }
    public string theCarModel { get; set; }
}

public class HomeController : Controller
{
    public PartialViewResult getUpdate(int carId)
    {
        CarModel carModel = new CarModel();
        switch (carId)
        {
            case 1:
                carModel.CarId = 1;
                carModel.CarMake = "updated11111Make";
                carModel.theCarModel = "updated11111Model";
                break;
            case 2:
                carModel.CarId = 2;
                carModel.CarMake = "updated2Make";
                carModel.theCarModel = "updated22222Model";
                break;
            case 3:
                carModel.CarId = 3;
                carModel.CarMake = "updated3Make";
                carModel.theCarModel = "updated33333Model";
                break;
            default:
                break;
        }
        return PartialView("_PartialView", carModel);
    }

    public ActionResult Index700()
    {
        IList<CarModel> carModelList = Setup();
        return View(carModelList);
    }
公共类CarModel
{
public int CarId{get;set;}
公共字符串CarMake{get;set;}
公共字符串加密模式{get;set;}
}
公共类HomeController:控制器
{
公共PartialViewResult获取更新(int carId)
{
CarModel CarModel=新CarModel();
开关(carId)
{
案例1:
carModel.CarId=1;
carModel.CarMake=“updated11111Make”;
carModel.theCarModel=“updated11111Model”;
打破
案例2:
carModel.CarId=2;
carModel.CarMake=“updated2Make”;
carModel.theCarModel=“updated222model”;
打破
案例3:
carModel.CarId=3;
carModel.CarMake=“updated3Make”;
carModel.theCarModel=“updated33333Model”;
打破
违约:
打破
}
返回PartialView(“PartialView”,carModel);
}
公共行动结果索引700()
{
IList carModelList=Setup();
返回视图(carModelList);
}
以下是您的看法:

    @model IEnumerable<Testy20161006.Controllers.CarModel>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index700</title>
    @*I had to add these references*@
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".getClick").click(function () {
                alert("ap");
                $(".clearMe").text("");
            })
        })
    </script>
</head>
<body>
    <table>
        @foreach (var car in Model)
        {
            <tr>
                <td>
                    @car.CarId
                </td>
                <td>
                    @car.CarMake
                </td>
                <td>
                    @car.theCarModel
                </td>
                <td>
                    @if (@car.CarId == 1)
                    {
                        <div id="result1" class="clearMe"></div> @*//update result based on ID*@
                    }
                    else if (@car.CarId == 2)
                    {
                        <div id="result2" class="clearMe"></div> @*//update result based on ID*@
                    }
                    else
                    {
                        <div id="result3" class="clearMe"></div> @*//update result based on ID*@
                    }
                </td>
                <td>
                    Check for @Ajax.ActionLink(
                  @car.CarMake,
                               "getUpdate",
                               new { carId = car.CarId },
                               new AjaxOptions
                                {
                                    UpdateTargetId = "result" + car.CarId, //use car.ID here? not sure
                                    InsertionMode = InsertionMode.Replace,
                                    HttpMethod = "GET"
                                }, new { @class = "getClick" })
                </td>
            </tr>
        }
    </table>
</body>
</html>
@model IEnumerable
@{
布局=空;
}
Index700
@*我不得不添加这些参考资料*@
$(函数(){
$(“.getClick”)。单击(函数(){
警报(“ap”);
$(“.clearMe”).text(“”);
})
})
@foreach(模型中的var车辆)
{
@car.CarId
@卡马克
@汽车.装甲
@如果(@car.CarId==1)
{
@*//基于ID更新结果*@
}
否则,如果(@car.CarId==2)
{
@*//基于ID更新结果*@
}
其他的
{
@*//基于ID更新结果*@
}
检查@Ajax.ActionLink(
@卡马克,
“获取更新”,
新{carId=car.carId},
新选择
{
UpdateTargetId=“result”+car.CarId,//在此处使用car.ID?不确定
InsertionMode=InsertionMode.Replace,
HttpMethod=“获取”
},新建{@class=“getClick”})
}

检查为ActionLink生成的Html。对于jquery中的ID属性,您需要执行
$(element).attr(“ID”)
当您单击操作链接并在操作上仅收到第一个ID时?@rem如果找到答案,请告诉我。
 public class CarModel
{
    public int CarId { get; set; }
    public string CarMake { get; set; }
    public string theCarModel { get; set; }
}

public class HomeController : Controller
{
    public PartialViewResult getUpdate(int carId)
    {
        CarModel carModel = new CarModel();
        switch (carId)
        {
            case 1:
                carModel.CarId = 1;
                carModel.CarMake = "updated11111Make";
                carModel.theCarModel = "updated11111Model";
                break;
            case 2:
                carModel.CarId = 2;
                carModel.CarMake = "updated2Make";
                carModel.theCarModel = "updated22222Model";
                break;
            case 3:
                carModel.CarId = 3;
                carModel.CarMake = "updated3Make";
                carModel.theCarModel = "updated33333Model";
                break;
            default:
                break;
        }
        return PartialView("_PartialView", carModel);
    }

    public ActionResult Index700()
    {
        IList<CarModel> carModelList = Setup();
        return View(carModelList);
    }
    @model IEnumerable<Testy20161006.Controllers.CarModel>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index700</title>
    @*I had to add these references*@
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".getClick").click(function () {
                alert("ap");
                $(".clearMe").text("");
            })
        })
    </script>
</head>
<body>
    <table>
        @foreach (var car in Model)
        {
            <tr>
                <td>
                    @car.CarId
                </td>
                <td>
                    @car.CarMake
                </td>
                <td>
                    @car.theCarModel
                </td>
                <td>
                    @if (@car.CarId == 1)
                    {
                        <div id="result1" class="clearMe"></div> @*//update result based on ID*@
                    }
                    else if (@car.CarId == 2)
                    {
                        <div id="result2" class="clearMe"></div> @*//update result based on ID*@
                    }
                    else
                    {
                        <div id="result3" class="clearMe"></div> @*//update result based on ID*@
                    }
                </td>
                <td>
                    Check for @Ajax.ActionLink(
                  @car.CarMake,
                               "getUpdate",
                               new { carId = car.CarId },
                               new AjaxOptions
                                {
                                    UpdateTargetId = "result" + car.CarId, //use car.ID here? not sure
                                    InsertionMode = InsertionMode.Replace,
                                    HttpMethod = "GET"
                                }, new { @class = "getClick" })
                </td>
            </tr>
        }
    </table>
</body>
</html>