Asp.net mvc 4 MVC 4 Html.ActionLink未向控制器传递参数

Asp.net mvc 4 MVC 4 Html.ActionLink未向控制器传递参数,asp.net-mvc-4,actionresult,Asp.net Mvc 4,Actionresult,我对MVC有些陌生,正在尝试将登录页面重写为MVC。 我无法将参数传递到控制器中的ActionResult,传入的参数为null 这里是观景台 <div class="form-group"> <div class="row"> @Html.TextBoxFor(model => model.UserName) </div> </div> <button class="btn btn-primary"> @Html.Acti

我对MVC有些陌生,正在尝试将登录页面重写为MVC。 我无法将参数传递到控制器中的ActionResult,传入的参数为null

这里是观景台

 <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>
我查了其他的帖子,我确信我使用了适当的重载

这里我添加了路由配置

  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

单击链接将发出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]
public ActionResult LogIn(Login model)
{
  // do something with model.UserName and model.Password
  // to do : return something
}

Model.UserName
是非空值吗?@Shyju如果你是指我的型号?是的,它不是一个可空字段公共类登录{public string UserName{get;set;}公共字符串密码{get;set;}}您的代码在我看来很好。您是否更改了默认路由定义?否。我的意思是Model.UserName的值property@Shyju-是的,值为null,我编辑了post以显示我在上面尝试过的配置在我的控制器中仍然获得null参数在我设法传递参数后,我将更新此post,正在尝试在此处获取骨骼:)你确定你的获取操作请求在querystring中包含用户名和密码吗?现在刚刚选中fiddler,没有输入任何查询字符串:/我想你需要修复它。我认为您需要了解如何将数据从操作方法传递到视图。看看感谢你的链接,在我的例子中,我试图将数据从文本框传递到我的actionResult。如果我将日期从ActionResult传递到view:,它可以正常工作。)
[HttpPost]
public ActionResult LogIn(string userName, string password)
{
  // do something with the posted data and return something
}
[HttpPost]
public ActionResult LogIn(Login model)
{
  // do something with model.UserName and model.Password
  // to do : return something
}