1

我想将 div 的 id 传递给控制器​​。我已将操作链接附加到 div,我正在尝试执行以下操作,

我的 TasksController 的索引视图

@Html.ActionLink("c", "Create", "TasksController", new { i = 6 }, new { @class = "element" })

在创建控制器内部

public ActionResult Create(string i)
        {
            ViewData["I"] = i;
            return View();
        } 

然后在创建视图中,

<div class="editor-field">
            @Html.TextBox("divID", ViewData["I"])
            @Html.ValidationMessageFor(model => model.divID)
</div>

但这不起作用。请问有什么帮助吗?提前致谢。

4

1 回答 1

1

尝试这个:

@Html.ActionLink("c", "Create", "Tasks", new { i = "6" }, new { @class = "element" })

您也可以通过配置路由来解决此问题:

全球.asax:

routes.MapRoute(
    "Default",     // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

动作链接:

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- Controller Name.
                new { "6"}, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

链接将如下所示:

<a href="/Item/Login/6">Title</a> 

请考虑控制器名称是“Tasks”而不是“Taskscontroller”。

编辑: 要重定向到另一个视图并传递一些数据,您必须在控制器中使用“RedirectToAction”:

return RedirectToAction("Tests", new { 
   ID = "6", 

});

编辑

你必须写“6”,只是任务希望这会奏效!

于 2012-05-02T06:40:37.333 回答