简体   繁体   English

如何将值从视图传递到Asp.net中的控制器

[英]How to pass values from view to controller in Asp.net

So we have this form in Login.cshtml 所以我们在Login.cshtml中有此表格

<form class="form-horizontal" action="LoginTest">
        <div class="form-group">
            <label for="inputEmail" class="col-sm-2 control-label">Gebruikersnaam</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputEmail" placeholder="Email">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword" class="col-sm-2 control-label">Wachtwoord</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="inputPassword" placeholder="Password">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Registreer</button>
                <button type="submit" class="btn btn-default">Annuleer</button>
            </div>
        </div>
    </form>

And we have a Logincontroller with the next method LoginTest: 我们有一个Logincontroller和下一个方法LoginTest:

public ActionResult LoginTest(string inputEmail, string inputPassword)
    {
        Response.Write("<script>alert('" + inputEmail + "')</script>");
        return null;
    }

But we are unable to pass the values. 但是我们无法传递这些值。 Hopefully someone can help us! 希望有人可以帮助我们!

You need to pass those values through the name tag, not id tag, like this: 您需要通过name标签而不是id标签传递这些值,如下所示:

<form class="form-horizontal" action="LoginTest">
        <div class="form-group">
            <label for="inputEmail" class="col-sm-2 control-label">Gebruikersnaam</label>
            <div class="col-sm-10">
                <input type="text" name="inputEmail" class="form-control" id="inputEmail" placeholder="Email">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword" class="col-sm-2 control-label">Wachtwoord</label>
            <div class="col-sm-10">
                <input type="password" name="inputPassword" class="form-control" id="inputPassword" placeholder="Password">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Registreer</button>
                <button type="submit" class="btn btn-default">Annuleer</button>
            </div>
        </div>
    </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM