如何从MVC中的ActionLink获取ID

时间:2014-08-26 09:20:11

标签: asp.net-mvc

我刚刚说过工作MVC所以我不知道我从动作链接获取ID和名称我有动作链接它有我想要的名称和ID我将在下一页重定向它我需要这些ID和名称那里... thanx提前

    <div class="CSSTableGenerator">
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CmpName)
            </th>
            <th>
                Select Company
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CmpName)
                </td>
                <td>
                    @Html.ActionLink("Select Company", "UserDashBoard", "User", new { 
                    item.CmpName,item.ID })
                </td>
            </tr>
        }
    </table>
</div>

2 个答案:

答案 0 :(得分:1)

以下是您想要的代码

查看旁边代码: -

@Html.ActionLink("Select Company", "UserDashBoard", "User", new { 
                id=item.ID,name=item.CmpName, })

控制器端代码: -

    public ActionResult UserDashBoard (int? id, string name)
    {

答案 1 :(得分:1)

直接从项目中取出: //我在表格中有一份文件清单:

foreach( var item in Model.DocumentList)
{

    <td>
    @Html.ActionLink( "Delete","Delete", "Document",new{ id= item.Id, tenancyId = item.TenancyId}, null );
    <td>
}

在这种情况下,ID永远不应为null,因为我们从文档列表中的项目中获取它,因此它不需要是Nullable

public ActionResult Delete(int id, int tenancyId)
{
    var model = new DeleteViewModel();
    model.PageTitle = "example";
    model.PageHeader = "example";

    var documents = _db.Document.Single(a=>a.Id == id);

    _db.Document.Remove(documents);

    return View(model)
}