2

我是 MVC 新手,我想在 MVC4 中创建一个“注册表单”

  1. 我创建了一个“注册表单” ,它在单击提交时将数据添加到数据库表“注册表” 。

  2. 我在“Registration-Form”下方的同一页面上有一个“Web-Grid” ,它显示了数据库表“Registration”中存在的数据。

两种操作在不同的视图中都可以正常工作。但是当我试图在一个视图中实现时,就会出现问题。

第一次操作需要正常的模型变量。

第二次操作需要 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 回答