0

我有一个视图,<input type="submit" value="Create" />当用户单击创建时,应该激活操作方法并将结果写入数据库。

在用户单击视图中的创建按钮时,什么也没有发生。你能告诉我我做错了什么吗?谢谢

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestGuestBook.Models;
using TestGuestBook.Models.Repositories;
using TestGuestBook.ViewModels;

namespace TestGuestBook.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {

        ICommentRepository _repository;

        public HomeController()
        {
            _repository = new CommentRepository();
        }

        // Dependency Injection enabled constructors
        public HomeController(ICommentRepository repository)
        {
            _repository = repository;
        }

        public ActionResult Index()
        {
            // Get all Comments
            List<Comment> commentItems = _repository.FindAll().ToList();
            // Create the ViewModel and associate the list of comments
            CommentListCreateViewModel viewModel = new CommentListCreateViewModel();
            viewModel.CommentItems = commentItems;

            return View(viewModel);
        }

        public ActionResult Create()
        {
            CommentListCreateViewModel createViewModel = new CommentListCreateViewModel();
            return View(createViewModel);
        }

        [HttpPost]
        public ActionResult Create(CommentListCreateViewModel createViewModel)
        {
            if (ModelState.IsValid)
            {
                Comment comment = new Comment
                {
                    Nominative = createViewModel.Nominative,
                    Email = createViewModel.Email,
                    Content = createViewModel.Content
                };
                _repository.Add(comment);
                _repository.Save();
            }
            return View();
        }

    }
}

看法

@model TestGuestBook.ViewModels.CommentListCreateViewModel
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ListAddCommentsViewModel</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Nominative)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nominative)
            @Html.ValidationMessageFor(model => model.Nominative)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Content)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Content)
            @Html.ValidationMessageFor(model => model.Content)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<table>
    <tr>
        <th>
            Nominative
        </th>
        <th>
            Email
        </th>
        <th>
            Content
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model.CommentItems)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nominative)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
            </td>
        </tr>

    }
</table>
4

2 回答 2

0

您需要将表单定向到您的Create控制器方法:

@using (Html.BeginForm("Create", "Home"))
于 2012-07-19T20:07:46.693 回答
0

您可以将其保留为 Html.BeginForm() 并在保存后调用 return RedirectToAction("Index"); 添加的项目现在应该显示在列表中。它可能一直在保存,只是之后没有被重定向到索引视图。

于 2012-07-19T20:30:43.200 回答