ASP.NET MVC:在控制器操作方法中获取文本框输入

时间:2014-11-10 17:58:14

标签: asp.net-mvc model-view-controller asp.net-mvc-5

我在一个页面和一个按钮中有两个文本框。现在,当用户单击按钮时,我想在控制器的操作方法中传递用户输入的文本。这可能很傻。但我是MVC的新手。请告诉我如何做到这一点。这是我尝试过的代码。

查看

<input type="text" name="input_domain_id" class="form-control text-center" placeholder="Domain ID" autofocus required>
            <input type="password" name="input_domain_password" class="form-control text-center" placeholder="Domain Password" required>
        <div class="col-lg-12">
            <p><button class="btn btn-lg btn-success btn-block" type="submit" onclick="location.href='@Url.Action("VerifyLogin", "LdapLoginVerify")'">Login</button></p>
            </div>

并在控制器中

 public ActionResult VerifyLogin(string input_domain_id, string input_domain_password)
        {
    return(view);
}

在我的代码中,我在两个参数中都得到空值。 请告诉我该怎么做.p

1 个答案:

答案 0 :(得分:2)

表格需要HTML。执行此操作的最佳方法是使用MVC帮助程序:

@using (Html.BeginForm())
{
    <input type="text" name="input_domain_id" class="form-control text-center" placeholder="Domain ID" autofocus required>
    <input type="password" name="input_domain_password" class="form-control text-center" placeholder="Domain Password" required>
    <div class="col-lg-12">
        <p><button class="btn btn-lg btn-success btn-block" type="submit" onclick="location.href='@Url.Action("VerifyLogin", "LdapLoginVerify")'">Login</button></p>
    </div>
}

你很可能也想确保你有两个动作。

[HttpGet]
public ActionResult VerifyLogin()
{
    return View(/* optional model with prepopulated data */);
}

[HttpPost]
public ActionResult VerifyLogin(string input_domain_id, string input_domain_password)
{
    // Do stuff with posted values...

    return View();
}

您还应该考虑使用帮助器输入,按钮等......

@ Html.EditorFor(),@ Html.PasswqordFor等。