MVC表单提交赢取发送选择标签选择选项

时间:2017-05-23 07:35:24

标签: javascript asp.net-mvc forms html-select

我有一个带有HTML select标签的MVC表单。 select是从JavaScript填充的。 如何使用表单提交发送所选选项?

@using (Html.BeginForm("LoginFromTest", "Login", FormMethod.Post))
{
    @Html.ValidationSummary(true, "Login failed. Check your login details.");
    <div style="margin: 10px;">
        <fieldset>
            <legend>Login</legend>

            <div class="editor-label">
                Username: 
            </div>
            <div class="editor-field">
                <input type="text" name="user" />
            </div>

            <div class="editor-label">
                Password: 
            </div>
            <div class="editor-field">
                <input type="password" name="password" />
            </div>

            <div class="editor-label">
                Authority:
            </div>
            <div class="editor-field">
                <select id="authority" />
            </div>

            <input type="submit" value="Log In" />
            <br/>
            @if (!string.IsNullOrWhiteSpace(ViewBag.Error))
            {
                <span style="color: red;">@ViewBag.Error</span>
            }
        </fieldset>
    </div>
}

由于

1 个答案:

答案 0 :(得分:0)

假设您的操作方法类似于:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1">

  </head>

  <body>
  
  <div class="container text-center">
  <div class="row">
  <div class="col-sm-4 col-lg-3 col-md-4">
  <img class="img-responsive top" src="https://s-media-cache-ak0.pinimg.com/736x/06/cb/33/06cb338efcf3ac37a90caad05fd356a2.jpg">
    <p style="background-color:#fff;">Example headline.</p>
  </div>
   <div class="col-sm-4 col-lg-3 col-md-4">
  <img class="img-responsive top" src="https://s-media-cache-ak0.pinimg.com/736x/06/cb/33/06cb338efcf3ac37a90caad05fd356a2.jpg">
  </div>
    <div class="col-sm-4 col-lg-3 col-md-4">
  <img class="img-responsive top" src="https://s-media-cache-ak0.pinimg.com/736x/06/cb/33/06cb338efcf3ac37a90caad05fd356a2.jpg">
 
  
  </div>
  </div>

  </body>
  </html>

您只需要为您的选择提供与您希望填充的参数相匹配的名称:

public ActionResult LoginFromTest(string user, string password, string authority)

如果你的模型需要绑定到模型:

 <div class="editor-field">
    <select id="authority" name="authority">
        <option value="X" />
        <option value="Y" />
    </select>
</div>

您的行动将如下所示:

public class myModel
{
   public string username {get; set;}
   public string password {get; set;}
   public string authority {get; set;}
}

但名称保持不变。您也可以使用@Html.DropDowListForpublic ActionResult LoginFromTest(myModel model) 帮助程序,但完全可以使用平面HTML执行此操作。