2015-11-08 58 views
0

遇到問題創建一個視圖和它自己的ActionResult。我得到的錯誤是在「/」應用這一個錯誤404在asp.net MVC。查看我不是顯示

服務器錯誤。

找不到資源。

說明:HTTP 404。您正在尋找(或它的依賴 之一)的資源可能已被刪除,重命名或沒有 暫時不可用。檢查以下URL並 確保其正確寫入。

請求的URL:/課程/ AgregarAlumno/2

這是我的看法(我知道它的未完成的,但我認爲那不是問題)

@model LibreriaEntidad.Cursos 
@{ 
    ViewBag.Title = "AgregarAlumnos"; 
} 

<h2>AgregarAlumnos</h2> 

@using (Html.BeginForm()) 
{ 
    <fieldset> 
     <legend>Cursos</legend> 

     @*<div class="editor-label"> 
      @Html.LabelFor(model => model.idCurso) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.idCurso) 
      @Html.ValidationMessageFor(model => model.idCurso) 
     </div>*@ 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Nombre) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Nombre) 
      @Html.ValidationMessageFor(model => model.Nombre) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.idReparticion) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.idReparticion) 
      @Html.ValidationMessageFor(model => model.idReparticion) 
     </div> 

     @*<div class="editor-label"> 
      @Html.LabelFor(model => model.Presencual) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Presencual) 
      @Html.ValidationMessageFor(model => model.Presencual) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Virtual) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Virtual) 
      @Html.ValidationMessageFor(model => model.Virtual) 
     </div>*@ 

      <div class="editor-field"> 
       @Html.DropDownList("idUsuario", ViewBag.Usuarios as SelectList) 
      </div> 
      <p> 
       <input type="submit" value="Save" /> 
      </p> 
</fieldset> 

和我控制器操作過在這裏。

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult AgregarAlumnos(int id = 0) 
{ 
    Cursos cursos = db.Cursos.Find(id); 
    ViewBag.Usuarios = new SelectList(db.Usuario, "idUsuario", "Nombre"); 
    return View(cursos); 
} 

在此先感謝

+0

你試圖'/場/ AgregarAlumno'但你的行動is' AgregarAlumnos'的名稱(用一個 's')。此外,您使用POST訪問,或得到什麼? – haim770

+0

我改了名字,並添加「S」,但錯誤力離開。它是一個POST方法。 –

+0

在任何「區」的控制器? – Azim

回答

0

你應該從你的行動中刪除[HttpPost]屬性。

更新:

顯示要查看的動作必須接受GET請求。您應該使用用於顯示視圖和提交形成兩個不同的動作。

+0

任何解釋,爲什麼?我有點剛從編輯動作複製,導致同一種ITS –

+1

由於顯示的視圖,動作必須是GET的結果。當您提交表單使用POST。 – Chandru

0

HttpGet通訊您需要一種方法,作爲當前一個只有HttpPost接受的請求。添加以下內容:

[HttpPost] 
public ActionResult AgregarAlumnos(int id = 0) 
{ 
    Cursos cursos = db.Cursos.Find(id); 
    ViewBag.Usuarios = new SelectList(db.Usuario, "idUsuario", "Nombre"); 
    return View(cursos); 
} 

HTTPGET: -每當任何負荷首次HttpGet查看執行方法。這意味着在Asp.Net MVC HttpGet執行方法的任何URL第一次請求。這意味着,如果任何URL接收輸入網址,通過我們可以通過HttpGet方法處理輸入。

HttpPost: -HttpPost方法執行你點擊MVC形式提交11按鈕。這意味着我們可以通過使用HttpPost方法處理表單數據。