2016-10-05 19 views
0

私は、私は非常に長い時間が経過した後、フロントエンドの開発を行うと、フロントエンドに取り組んでいますStudentController、Index.cshtmlと中央layout.cschtmlと呼ばれるコントローラ、どのように、同じコントローラ上の別のアクションにビューをリダイレクトする

を持っていますasp.net mvcは静かな私のための挑戦ですので、何かが間違っている場合は私を許してください。

私が達成しようとしているのは、自分のアプリケーションを実行しているときに完璧に実行されますが、リンクをクリックすると学生が正常にStudent Indexビューに移動し、StudentControllerのIndexメソッドにヒットします。

しかし、私はIndexビューに新しいリンクを作成をクリックしたとき、それはエラーになります(一番下を参照)、誰かが私が間違っているのものを導くことができます....

StudentController

public StudentController(){} 
public ActionResult Index() 
{    
    var students =_studentDb.GetAll();    
    return View(students); 
} 

[HttpPost] 
public ActionResult Create(Student student) 
{ 
    var result = _studentDb.Insert(student); 
    return View("Create"); 
} 

_layout.chtml

<ul class="nav navbar-nav"> 
        <li>@Html.ActionLink("Home", "Index", new { Controller = "Home", Area = "Common" })</li>      
        <li>@Html.ActionLink("Student", "Index", new { Controller = "Student", Area = "User" })</li> 
        <li>@Html.ActionLink("New Student", "Index", new { Controller = "CreateStudent", Area = "User" })</li> 
       </ul> 

Index.chtml

@model IEnumerable<Student> 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create",new {Controller="Student",Area="User" })   
</p> 
<table class="table"> 
    <tr>   
     <th> 
      @Html.DisplayNameFor(model => model.FirstName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.MiddleName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.LastName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Gender) 
     </th>   
     <th> 
      @Html.DisplayNameFor(model => model.FirstNameAr) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.MiddleNameAr) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.LastNameAr) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr>   
     <td> 
      @Html.DisplayFor(modelItem => item.FirstName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.MiddleName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.LastName) 
     </td>   
     <td> 
      @Html.DisplayFor(modelItem => item.FirstNameAr) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.MiddleNameAr) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.LastNameAr) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
     </td> 
    </tr> 
} 

</table> 

エラー

Server Error in '/' Application. 

The resource cannot be found. 

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /User/Student/Create 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0 
+1

作成と呼ばれるビューがありますか? –

+3

問題は、 'POST'メソッドを' GET'しようとしていることです。あなたはCreateと呼ばれる '[HttpPost]'アクションを持っています - これはあなたが学生を作るためにPOSTするものです。また、ユーザ入力のためのフォームである '[HttpGet]' Createメソッドが必要です。 – markpsmith

+0

@PaulKaram、はい私はCreateと呼ばれるビューを持っています – Shax

答えて

1

まず、コントローラにはGET用とPOST用の2つのメソッドが必要です。あなたはPOSTしか持っていません。

GETはあなたがここにある作成ビューを返すので、HttpPost属性を変更してください。

[HttpGet] 
public ActionResult Create() 
{ 
    return View("Create"); 
} 

あなたが代わりに使用すると、2つのアクションを必要とする一般的に、この

public ActionResult Create() 
{ return View(); } 

[HttpPost] 
public ActionResult Create(Student student) 
{ 
    var result = _studentDb.Insert(student); 
    return View(); 
} 
+0

GETメソッドは、学生をデータベースに挿入してはいけません(最初の行を取り除く) –

+0

ありがとうespCoder、それは働いたうまく – Shax

+0

キャッチ@マークありがとう。私は答えを更新しました。 – espCoder

2

あなたはポストアクションに<a>タグを生成しているので、あなたがエラーを取得しています。あなたはフォームの投稿やjavascriptのajaxを使って投稿アクションにアクセスすることができます。そのような別のビューにリンクしようとしている場合は、[HttpPost]属性を削除する必要があります。さらに、を作成ビューに渡すといいでしょう:return View(result)(ビューにはアクションと同じ名前があるため、このオーバーロードが使用されます)。さらに、CreateアクションにはStudentパラメータがありますが、値を渡すことはありません。私はより多くの次のような構造をお勧めします:

public IActionResult Create() 
{ 
    var student = new Student(); 
    return View(student) 
} 

[HttpPost] 
public IActionResult Create(Student student) 
{ 
    _studentDb.Add(student); 
    _studentDb.SaveChanges(); 
    // Do whatever you like to finish this action 
} 

そしてあなたの作成ビューの作成/ /ユーザー/学生への投稿しまう生徒に関するデータを入力するためのフォームが含まれている必要があります。

2

変更コードの)(RedirectToActionを返し、コントローラのアクションBを返すために、コントローラのアクションAにしたい場合:

  1. ユーザーが 値neを入力する画面を示すGETアクション新しいエンティティを作成するために編集します。これはフォーム に送信されます。
  2. 偽造トークンをチェックし、実際のデータ操作を行います( ユーザーをデータベースに追加します) 。追加のベストプラクティスは、あなたがすることはできません...その後 はあなたが唯一のPOSTアクションを持つ (https://en.wikipedia.org/wiki/Post/Redirect/Get

を掲示ダブル避けるために、GETアクション(バックインデックス アクションに例えばRedirectToAction)にリダイレクトすることですそのリンクをフォームとして提出する必要があります。したがって、GETアクションも作成する必要があります。

1

のようなビュー()

+0

あなたのコメントを理解しました – Shax

関連する問題