2

私は MVC が初めてで、MVC4 で「登録フォーム」を作成しようとしています。

  1. 送信をクリックするとデータベーステーブル「登録」にデータを追加する「登録フォーム」を作成しました。

  2. データベーステーブル'Registration'に存在するデータを表示する'Registration-Form' のすぐ下の同じページに' Web-Grid'があります。

両方の操作は、異なるビューで正常に機能します。しかし、1 つのビューで実装しようとすると、問題が発生します。

最初の操作には通常のモデル変数が必要です。

2 番目の操作には IEnumerable モデル変数が必要です。

この問題を解決するにはどうすればよいですか。

これが私のコードです:

コントローラ:

    namespace RegistrationMVC.Controllers
    {
    public class RegistrationController : Controller
    {
        //
        // GET: /Registration/

        public ActionResult Index(Registration model)
        {
            UserContext dbContext = new UserContext();
            var details = dbContext.Registration.ToList();
            return View(details);
        }
        public enum Command
        {
            Create,
            Reset
        }

         [HttpPost]
        public ActionResult Index(Registration model, Command command)
        {
            if (command == Command.Create)
            {
                if (ModelState.IsValid)
                {
                    UserContext dbContext = new UserContext();

                    var newRegistration = new Registration() { Email = model.Email, Password = model.Password, FName = model.FName, LName = model.LName, Contact = model.Contact, Address = model.Address };
                    dbContext.Registration.Add(newRegistration);
                    dbContext.SaveChanges();

                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
            return View(model);
        }
    }
}

インデックス ビュー:

@model IEnumerable<RegistrationMVC.Models.Registration>

@{
    ViewBag.Title = "Index";
    var grid = new WebGrid(Model);
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @Html.Partial("_NewRegistration")
    @grid.GetHtml()
</body>
</html>

登録部分ビュー:

@model RegistrationMVC.Models.Registration

<script src="~/Scripts/jquery-2.0.3.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "Registration Failed. Please correct the errors and try again.")
    <div>
        <fieldset>
            <legend>Registration</legend>

            @Html.LabelFor(model => model.Email, "Email:")
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
            <br />

            @Html.LabelFor(model => model.Password, "Password:")
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
            <br />

            @Html.LabelFor(model => model.FName, "First Name:")
            @Html.EditorFor(model => model.FName)
            @Html.ValidationMessageFor(model => model.FName)
            <br />

            @Html.LabelFor(model => model.LName, "Last Name:")
            @Html.EditorFor(model => model.LName)
            @Html.ValidationMessageFor(model => model.LName)
            <br />

            @Html.LabelFor(model => model.Contact, "Contact No:")
            @Html.EditorFor(model => model.Contact)
            @Html.ValidationMessageFor(model => model.Contact)
            <br />

            @Html.LabelFor(model => model.Address, "Address:")
            @Html.TextAreaFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
            <br />

            <input type="submit" name="Command" value="Create" />
            <input type="submit" name="Command" value="Reset" />

    </fieldset>
    </div>
}
4

1 に答える 1

0

部分ビューを使用し、必要に応じて特定の部分ビューにモデルを提供しています

于 2013-10-11T07:00:49.073 に答える