NET MVC 向控制器传递 ActionLink 中的 ID

我似乎无法检索发送到 html 中的 ID。在我的控制器中的 ActionLink,下面是我正在尝试做的

<li>
<%= Html.ActionLink("Modify Villa", "Modify", "Villa", new { @id = "1" })%></li>




public ActionResult Modify(string ID)
{


ViewData["Title"] =ID;
return View();
}

这是我遵循的一个教程推荐的,但它不工作,它也把?URL 末尾的长度 = 5!

这是我使用的路线,是默认的

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

Don't put the @ before the id

new { id = "1" }

The framework "translate" it in ?Lenght when there is a mismatch in the parameter/route

Doesn't look like you are using the correct overload of ActionLink. Try this:-

<%=Html.ActionLink("Modify Villa", "Modify", new {id = "1"})%>

This assumes your view is under the /Views/Villa folder. If not then I suspect you need:-

<%=Html.ActionLink("Modify Villa", "Modify", "Villa", new {id = "1"}, null)%>

In MVC 4 you can link from one view to another controller passing the Id or Primary Key via

@Html.ActionLink("Select", "Create", "StudentApplication", new { id=item.PersonId }, null)

The ID will work with @ sign in front also, but we have to add one parameter after that. that is null

look like:

@Html.ActionLink("Label Name", "Name_Of_Page_To_Redirect", "Controller", new {@id="Id_Value"}, null)

On MVC 5 is quite similar

@Html.ActionLink("LinkText", "ActionName", new { id = "id" })

If the target action requires a parameter, you can use an anonymous object to pass parameter values:

@Html.ActionLink(“View Movies”, “Index”, “Movies”, new{id=1},null)  

Where:

-View Movies-->String LinkText

-Index--> string ActionName

-Movies-->string ControllerName

-new{id=1}-->(object) Is the parameter values that you want to pass

-Null-->object htmlAttributes

*For Example:

This should generate a link like the following:


/movies/index/1