【问题标题】:How to Post data to database and Bind it to web-grid in same view?如何将数据发布到数据库并在同一视图中将其绑定到网络网格?
【发布时间】:2013-10-03 12:23:50
【问题描述】:

我是 MVC 新手,我正在尝试在 MVC4 中创建一个'Registration-Form'

  1. 我创建了一个'Registration-Form',它将数据添加到数据库表'Registration' 点击提交。

  2. 我有一个'Web-Grid',位于'Registration-Form'的下方 显示数据库表 '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>
}

【问题讨论】:

  • 你在用ajax吗?
  • 不能只使用控制器。
  • 我无法从您的问题中判断您是否需要:1.在不刷新页面的情况下在gridview中显示新提交的注册,或者2.提交新注册,刷新页面(即,做一个 POST),然后 gridview 将显示新的注册。主要区别在于,对于#1,您需要 AJAX 并强烈推荐淘汰赛,而#2 您只需要调整 POST 后返回的模型(可能创建一个视图模型)。

标签: c# asp.net-mvc-4 ef-code-first


【解决方案1】:

我有使用局部视图并根据需要为特定局部视图提供模型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多