2012-07-19 42 views
0

我有一個查看與<input type="submit" value="Create" />當用戶點擊創建操作方法應激活和結果寫入分貝。如何從視圖中調用方法? MVC

當用戶在視圖中單擊創建按鈕什麼都沒有發生。你能告訴我我做錯了什麼嗎?感謝

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> 

回答

0

您需要的形式直接到您的Create控制器的方法:

@using (Html.BeginForm("Create", "Home")) 
+0

對不起,我應該把這段代碼放在View中? – GibboK 2012-07-19 20:09:28

+0

用我發佈的內容替換'@using(Html.BeginForm())'這一行。如果不在表單中指定Action,它將回發到相同的URL,並且它將簡單地調用Index操作。 – 2012-07-19 20:18:55

+0

我做了,現在我收到一個錯誤未找到'創建'或其主人的視圖或沒有視圖引擎支持搜索的位置。搜索了以下位置:...我不確定我在這裏做錯了什麼。我需要發佈在相同的URL – GibboK 2012-07-19 20:23:00

0

你可以把它作爲Html.BeginForm()和後保存,調用返回RedirectToAction(「Index」);添加的項目現在應該顯示在列表中。它可能一直在保存,之後它不會被重定向到索引視圖。

+0

表單發佈到索引,所以只留下'BeginForm'將不起作用。 – 2012-07-19 20:36:16

+0

啊,我錯過了這是索引視圖,而不是創建視圖。 :-) – 2012-07-20 11:58:42