【问题标题】:MVC View to controller PassingMVC 视图到控制器传递
【发布时间】:2012-09-27 09:45:48
【问题描述】:

我开始使用 mvc 并设计一个简单的登录过程。 我有两个输入用户名和密码的登录屏幕视图。 但显然无法获得如何将输入值从我的视图传递到控制器 我正在使用剃须刀。这是我的 sn-ps。

<table>
    <tr>
        <td>
            UserName:
        </td>
        <td>
            @Html.TextBox("userName")
        </td>
    </tr>
        <tr>
        <td>
            Password
        </td>
        <td>
            @Html.Password("Password")
        </td>
    </tr>
    <tr>
        <td colspan="2">
            @Html.ActionLink("login", "SignIn")
        </td>
    </tr>
</table>

我的控制器看起来像这样。(我可以使用操作链接重定向到控制器就好了。只是传递值。)

public ActionResult SignIn()
{
    //string userName = Request["userName"];
    return View("Home");
}

【问题讨论】:

    标签: c# .net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    您可以将上面的 html 内容包含在表单容器中,您将表单提交方法声明为POST

    @using (Html.BeginForm("SignIn", "Controller", FormMethod.Post, new { id = "form1" }))
    {
        <table>
        <tr>
            <td>
                UserName:
            </td>
            <td>
                @Html.TextBox("userName")
            </td>
        </tr>
            <tr>
            <td>
                Password
            </td>
            <td>
                @Html.Password("Password")
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="login" name="login" />
            </td>
        </tr>
    </table>
    }
    

    然后,您可以将Post Action 放入您的控制器中:

    [HttpPost]
    public ActionResult SignIn(FormCollection frmc)
    {
        /// Extracting the value from FormCollection
        string name = frmc["userName"];
        string pwd = frmc["Password"];
        return View("Home");
    }
    

    【讨论】:

    • 完美!这让我产生疑问。这是最佳做法吗?还是有更好的?
    • 我会使用@model VmLoginParams 制作视图,并使控制器发布操作采取public ActionResult SignIn(VmLoginParams params) 在Html 代码中您可以执行@Html.EditorFor(m =&gt; m.UserName) @Html.ValidationMessageFor(m =&gt; m.UserName) 并在控制器中检查ModelState.IsValid:@ 987654329@(另见:mikesdotnetting.com/Article/188/…
    【解决方案2】:

    将表格包装在表格中:

        @using (Html.BeginForm("SignIn", "controllerName", FormMethod.POST))
    {
    <table>
    ...
    </table>
    <input type="submit" value="Sign in" />
    }
    

    在控制器中写入:

    [HttpPost]
    public ActionResult SignIn(string userName, string Password)
    {
     //sign in and redirect to home page
    }
    

    【讨论】:

      【解决方案3】:

      查看:

      @using (Html.BeginForm("SignIn", "Controller", FormMethod.Post, new { id = "form1" }))
      {
      <table>
      <tr>
          <td>
              UserName:
          </td>
          <td>
              @Html.TextBox("userName")
          </td>
      </tr>
          <tr>
          <td>
              Password
          </td>
          <td>
              @Html.Password("Password")
          </td>
      </tr>
      <tr>
          <td colspan="2">
              <input type="submit" value="login" name="login" />
          </td>
      </tr>
      </table>
      }
      

      型号:

      public string userName{get;set;}
      public string Password{get;set;}
      

      控制器:

      [HttpPost]
       public ActionResult SignIn(Model obj)
       {
        //sign in and redirect to home page
         string userName = obj.username;
         string password = obj.password;
       }
      

      它可能对你很有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多