没有获取对象值或formcollection值

时间:2017-07-04 20:22:51

标签: asp.net-mvc

我正在尝试验证用户在MVC中的登录验证。我使用弹出窗口显示两个表单,一个用于登录,一个用于注册。即使它击中控制器,我也无法获得用户输入的对象值或文本框值(用户名和密码)。

        [HttpPost]
    public ActionResult CheckUser(UserInfo obj)
    {

        int res = udaObj.CheckUser(obj.UserName, obj.Password);
        if (res >= 1)
        {
            return RedirectToAction("Appointment", "Home");
        }
        else
        {
            //For testing purpose
            return RedirectToAction("Appointment", "Home");
        }

    }

我的cshtml如下:

                    @using (Html.BeginForm("RegisterUser", "Home", FormMethod.Post, new { id = "regForm" }))
                {

                    <div class="tab-pane fade" id="signup">
                        <fieldset>
                            <!-- Sign Up Form -->
                            <!-- Text input-->
                            <div class="control-group">
                                <label class="control-label" for="Email">Email:</label>
                                <div class="controls">
                                    @Html.TextBoxFor(x => x.Email, new { @class = "form-control input-large", @placeholder = "Joek@irawath.com", @required = "" })
                                </div>
                            </div>

                            <!-- Text input-->
                            <div class="control-group">
                                <label class="control-label" for="userid">Alias:</label>
                                <div class="controls">
                                    @Html.TextBoxFor(x => x.UserName, new { @class = "form-control input-large", @placeholder = "Joek@irawath.com", @required = "" })
                                </div>
                            </div>

                            <!-- Password input-->
                            <div class="control-group">
                                <label class="control-label" for="password">Password:</label>
                                <div class="controls">
                                    @Html.TextBoxFor(x => x.Password, new { @class = "form-control input-large", @placeholder = "********", @required = "", @type = "password" })

                                    <em>1-8 Characters</em>
                                </div>
                            </div>

                            <!-- Text input-->
                            <div class="control-group">
                                <label class="control-label" for="reenterpassword">Re-Enter Password:</label>
                                <div class="controls">
                                    <input id="reenterpassword" class="form-control" name="reenterpassword" type="password" placeholder="********" class="input-large" required="">
                                </div>
                            </div>



                            <!-- Button -->
                            <div class="control-group">
                                <label class="control-label" for="confirmsignup"></label>
                                <div class="controls">
                                    <button id="btnconfirmsignup" type="submit" name="signin" class="btn btn-success">Sign Up</button>
                                    @* <button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>*@

                                </div>
                            </div>
                        </fieldset>
                    </div>

                }

                @using (Html.BeginForm("CheckUser", "Home", FormMethod.Post, new { id = "loginForm" }))
                {
                    <div class="tab-pane fade active in" id="signin">
                        <fieldset>
                            <!-- Sign In Form -->
                            <!-- Text input-->
                            <div class="control-group">
                                <label class="control-label" for="userid">Alias:</label>
                                <div class="controls">
                                   @* <input required="" id="userid" name="userid" type="text" class="form-control" placeholder="JoeSixpack" class="input-medium" required="">*@
                                     @Html.TextBoxFor(x => x.UserName, new { @class = "form-control input-large", @placeholder = "Joek@irawath.com", @required = "" })
                                </div>
                            </div>

                            <!-- Password input-->
                            <div class="control-group">
                                <label class="control-label" for="passwordinput">Password:</label>
                                <div class="controls">
                                    @*<input required="" id="passwordinput" name="passwordinput" class="form-control" type="password" placeholder="********" class="input-medium">*@
                                     @Html.TextBoxFor(x => x.Password, new { @class = "form-control input-large", @placeholder = "********", @required = "", @type = "password"  })
                                </div>
                            </div>

                            <!-- Multiple Checkboxes (inline) -->
                            <div class="control-group">
                                <label class="control-label" for="rememberme"></label>
                                <div class="controls">
                                    <label class="checkbox inline" for="rememberme-0">
                                        <input type="checkbox" name="rememberme" id="rememberme-0" value="Remember me" style="margin-left: 0px">
                                        Remember me
                                    </label>
                                </div>
                            </div>

                          <div class="control-group">
                                <label class="control-label" for="confirmsignup"></label>
                                <div class="controls">
                                    <button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>
                                    @* <button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>*@

                                </div>
                            </div>
                        </fieldset>
                    </div>
                }

UserInfo.cs

    public class UserInfo
{


    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool Gender { get; set; }
    public DateTime? Dob { get; set; }
    public string PhNumber { get; set; }
    public string ImagePath { get; set; }
    public DateTime? RegDate { get; set; }
    public string Email { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我认为您的代码没有任何问题。

但我能猜到的是你的控制器没有遵循控制器的命名规则。您的控制器名称应具有“Controller”后缀。

在你的情况下它应该是

public class HomeController : Controller 
{
....
}

如果有帮助,请告诉我