简体   繁体   English

MVC ASP.NET中的错误404。 查看不显示

[英]Error 404 in mvc asp.net. View not display

Im having trouble creating a view and its own actionresult. 我在创建视图及其自己的动作结果时遇到麻烦。 The error that I get is this one 我得到的错误是这个

Error de servidor en la aplicación '/'. 错误的服务器错误'/'。

No se encuentra el recurso. 没有安全提示。

Descripción: HTTP 404. El recurso que está buscando (o una de sus dependencias) se puede haber quitado, haber cambiado de nombre o no estar disponible temporalmente. 描述:HTTP404。在总线上运行时,必须在haber quitado,haber cambiado de nombre或任何estar不负责任的时态下进行。 Revise la dirección URL siguiente y asegúrese de que está escrita correctamente. 修订URL siguiente yasegúresede queestáescrita Correctamente。

Dirección URL solicitada: /Cursos/AgregarAlumno/2 DirecciónURL solicitada:/ Cursos / AgregarAlumno / 2

This is my view (I know its unfinished, but I think thats not the problem) 这是我的观点(我知道它尚未完成,但我认为那不是问题)

@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>

and my controller action over here. 和我的控制器动作在这里。

[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);
}

Thanks in advance 提前致谢

You should remove [HttpPost] attributes from your action. 您应该从操作中删除[HttpPost]属性。

Update: 更新:

To display view the action must accept the GET request. 要显示视图,该操作必须接受GET请求。 You should use two different actions for displaying view and submitting form. 您应该使用两种不同的操作来显示视图和提交表单。

You need a corresponding HttpGet method, as your current one only accepts a HttpPost request. 您需要一个相应的HttpGet方法,因为您当前的方法仅接受HttpPost请求。 Add the following: 添加以下内容:

[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:- Whenever any View loads first time HttpGet method executes. HttpGet:-每当任何View首次加载时, HttpGet执行HttpGet方法。 That means on first request for any URL in Asp.Net MVC HttpGet method executes. 这意味着在首次请求Asp.Net MVC HttpGet方法中的任何URL时,将执行。 That means if any URL receives the input through URL we can handle the input through HttpGet method. 这意味着如果有任何URL通过URL接收输入,我们可以通过HttpGet方法处理输入。

HttpPost:- HttpPost method executes once you click on Submit button in MVC form. HttpPost:-单击MVC表单中的Submit按钮后,将执行HttpPost方法。 That means we can process the form data by using HttpPost method. 这意味着我们可以使用HttpPost方法处理表单数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM