0

私は MVC を初めて使用します。ログイン コントロールを作成したいので、以下のコードをビューに記述しました。

 @using (Html.BeginForm("LoginControl", "Login"))
{
    <div style="float: left; line-height: 36px;">
        @Html.ValidationSummary()
        <div style="float: left; margin-right: 20px;">
            UserName :
        </div>
        <div style="float: right;">@Html.TextBoxFor(o => o.login.UserName)
        </div>
        <br />
        <div style="float: left; margin-right: 20px;">
            Password &nbsp;&nbsp;:
        </div>
        <div style="float: right;">@Html.TextBoxFor(o => o.login.UserPassword)</div>
        <br />
        <div style="float: left; margin-right: 20px;">
            &nbsp;
        </div>
        <input type="button" value="Login" id="Login" title="Login" />
    </div>
}

コントローラーで私のコードは次のとおりです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication2.Controllers
{
public class LoginController : Controller
{
    //
    // GET: /Default1/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult LoginControl(String UserName, String UserPassword)
    {
        string result = "";
        if (UserName == "ram" && UserPassword == "ram123")
        {
            result = "1";
        }
        if (result == "1")
        {
            return View("LoginControl");
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password");
            return View("Index");
        }
    }
}

}

なぜそれが機能しているのかわかりません。助けてください。

4

1 に答える 1

0

まず、送信するボタンの入力タイプを変更します。

<input type="button" value="Login" id="Login" title="Login" />

ビュー コードを次のように変更します。

 @using (Html.BeginForm("LoginControl", "Login"))
{
    <div style="float: left; line-height: 36px;">
        @Html.ValidationSummary()
        <div style="float: left; margin-right: 20px;">
            UserName :
        </div>
        <div style="float: right;">@Html.TextBox("UserName")
        </div>
        <br />
        <div style="float: left; margin-right: 20px;">
            Password &nbsp;&nbsp;:
        </div>
        <div style="float: right;">@Html.TextBox("UserPassword")
        </div>
        <br />
        <div style="float: left; margin-right: 20px;">
            &nbsp;
        </div>
        <input type="submit" value="Login" id="Login" title="Login" />
    </div>
}

これがあなたを助けることを願っています。

于 2013-08-08T12:33:09.460 に答える