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

Asp.net mvc ASP.NET MVC:在控制器操作方法中获取文本框输入,asp.net-mvc,model-view-controller,asp.net-mvc-5,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="passwo

我有两个文本框在一个页面和一个按钮。现在,当用户单击按钮时,我想传递用户在控制器的操作方法中输入的文本。这可能很愚蠢。但我对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>
在我的代码中,两个参数中都有空值。
请告诉我怎么做。p

您需要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>
}
您还应该考虑为您的输入、按钮等使用帮助程序


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

注意:
onclick=“位置…”
需要从提交中删除button@StephenMuecke如果我没有在onclick事件中调用action方法,那么该action方法将如何调用?这是一个提交按钮,它会自动将表单中的值提交到forms
action
参数中指定的操作。您的
onclick
事件所做的只是在不传递任何参数的情况下进行GET调用(当然,它们是空的)。学习MVC。不要硬编码html,html使用帮助程序绑定到模型属性。发回一个模型,而不是单个参数等@StephenMuecke谢谢。你的评论有助于澄清我的概念。我现在肯定会先阅读mvc教程,然后再进一步,访问mvc站点,通过教程了解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();
}