MVC 4 Html.ActionLink 没有将参数传递给控制器
MVC 4 Html.ActionLink is not passing parameters to controller
我对 MVC 有点陌生,正在尝试将登录页面重写为 MVC。
我无法在控制器中将参数传递给我的 ActionResult,传入的参数为空。
这里是视图
<div class="form-group">
<div class="row">
@Html.TextBoxFor(model => model.UserName)
</div>
</div>
<button class="btn btn-primary">
@Html.ActionLink("GO!", "AppList", "LogIn", new { @userName = Model.UserName}, null)
</button>
我正在尝试将用户名和密码传递到我的控制器中。
public ActionResult AppList(string userName)
{
return View();
}
我查阅了其他 post,我确定我为此使用了适当的重载。
这里我添加了路由配置
routes.MapRoute(
name: "LogIn",
url: "{controller}/{action}/{id}",
defaults: new { controller = "LogIn", action = "Index", id = UrlParameter.Optional }
);
这是我正在加载登录页面的 actionResult
public ActionResult LogIn(string userName, string password)
{
ViewBag.LogInButton = "Log In";
return View(new Login());
}
并查看我分配的模型
@model LogInPortal.Controllers.LogInController.Login
点击link会发出GET请求,但不会提交表单数据。您的表单中需要一个提交按钮来提交表单字段值
@model LogInPortal.Controllers.LogInController.Login
@using(Html.BeginForm("Login","AppList"))
{
<div class="row">
@Html.TextBoxFor(model => model.UserName)
</div>
<div class="row">
@Html.TextBoxFor(model => model.Password)
</div>
<input type="submit" />
}
并使用 HttpPost 属性标记您的操作方法
[HttpPost]
public ActionResult LogIn(string userName, string password)
{
// do something with the posted data and return something
}
或者您甚至可以使用相同的登录 class 对象作为您的参数。默认模型联编程序会将发布的表单数据映射到该对象的 属性 值。
[HttpPost]
public ActionResult LogIn(Login model)
{
// do something with model.UserName and model.Password
// to do : return something
}
我对 MVC 有点陌生,正在尝试将登录页面重写为 MVC。 我无法在控制器中将参数传递给我的 ActionResult,传入的参数为空。
这里是视图
<div class="form-group">
<div class="row">
@Html.TextBoxFor(model => model.UserName)
</div>
</div>
<button class="btn btn-primary">
@Html.ActionLink("GO!", "AppList", "LogIn", new { @userName = Model.UserName}, null)
</button>
我正在尝试将用户名和密码传递到我的控制器中。
public ActionResult AppList(string userName)
{
return View();
}
我查阅了其他 post,我确定我为此使用了适当的重载。
这里我添加了路由配置
routes.MapRoute(
name: "LogIn",
url: "{controller}/{action}/{id}",
defaults: new { controller = "LogIn", action = "Index", id = UrlParameter.Optional }
);
这是我正在加载登录页面的 actionResult
public ActionResult LogIn(string userName, string password)
{
ViewBag.LogInButton = "Log In";
return View(new Login());
}
并查看我分配的模型
@model LogInPortal.Controllers.LogInController.Login
点击link会发出GET请求,但不会提交表单数据。您的表单中需要一个提交按钮来提交表单字段值
@model LogInPortal.Controllers.LogInController.Login
@using(Html.BeginForm("Login","AppList"))
{
<div class="row">
@Html.TextBoxFor(model => model.UserName)
</div>
<div class="row">
@Html.TextBoxFor(model => model.Password)
</div>
<input type="submit" />
}
并使用 HttpPost 属性标记您的操作方法
[HttpPost]
public ActionResult LogIn(string userName, string password)
{
// do something with the posted data and return something
}
或者您甚至可以使用相同的登录 class 对象作为您的参数。默认模型联编程序会将发布的表单数据映射到该对象的 属性 值。
[HttpPost]
public ActionResult LogIn(Login model)
{
// do something with model.UserName and model.Password
// to do : return something
}