如何在另一个表单值提交中获取表单值

时间:2013-04-25 17:31:32

标签: asp.net-mvc asp.net-mvc-3 forms razor

我在第一个表单中输入了用户名和密码,并以另一种形式检索密码,我希望在提交第二张表单时以第一种形式输入userame。

 @model Network.Models.LoginDetail

    <script type="text/javascript">

    </script>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <p>
        Please enter your user name and password. @Html.ActionLink("Register", "Register") if you don't have an account.
    </p>

    @using (Html.BeginForm("Index","Student",FormMethod.Post)) {
     <div>
            <fieldset>
                <legend>Account Information</legend>

                <div class="editor-label">
                @Html.LabelFor(model => model.Username)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Username)
                @Html.ValidationMessageFor(model => model.Username)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
            <p>
            <input type="submit" value="Log in" style="width: 200px"/>
            </p>
            @ViewBag.Message
            </fieldset>
             </div>
            }
            @using (Html.BeginForm("RetrievePassword","Student",FormMethod.Post)) {
            <p>
            Forgot password? <input type="submit" onclick="updateUsername();"value="Click Here" style="width: 150px"/> Entering your username to retrieve your password.
            </p>
            @Html.HiddenFor(model => model.Username)
        }   

我们怎样才能实现这一点,我尝试在隐藏字段中捕获用户名的javascript,但没有工作。 或者可以在表格内形成用于实现此目的。 请提供解决方案。

谢谢,

Michaeld

1 个答案:

答案 0 :(得分:0)

在“Stundent”控制器中的“索引”操作的post方法中,您将能够以formcollection或LoginDetail对象形式获取值。

示例:

public ActionRestult Index(FormCollection FC)
{
   string UserName = FC["Username"];
   string Password = FC["Password"];
}

或者

public ActionRestult Index(LoginDetail obj)
{
   string UserName = obj.Username;
   string Password = obj.Password;
}

这将解决您的问题。