C#表格不显示输入

时间:2015-03-07 21:58:48

标签: c# forms

我想在C#中构建一个表单,其中包含日,月和年的输入。

这是我到目前为止,表格有效,但输入并未在下一页显示。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication3.Controllers
{
     public class VerwerkController : Controller
     {
        // GET: Verwerk
        public ActionResult Index()
        {
             return View();
        }

        public ActionResult Verwerk(int dag, String maand, int jaar)
        {
            int day = dag;
            String month = maand;
            int year = jaar;

            return View(day + "/" + month + "/" + year);
        }
    }
}

这是VerwerkController.cs,下面的代码是Index.cshtml(View)。

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <form name="input" action="/home/verwerk" method="post" >
            Dag: <input type="number" name="dag" /><br />            
            Maand: <input type="text" name="maand" /><br />
            Jaar: <input type="number" name="jaar" /><br />
            <input type="submit" name="send" /><br />
        </form>
    </div>
</body>
</html>

刚开始使用C#而我不能解决这个问题,任何人都可以解决这个问题吗?

我现在收到了以下4个文件:

VerwerkModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication3.Models
{
    public class VerwerkModel
    {
        public int Dag { get; set; }
        public string Maand { get; set; }
        public int Jaar { get; set; }
    }
}

Verwerk.cshtml

@{
    Layout = null;
}
@Model WebApplication3.Models.VerwerkModel

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Verwerk</title>
</head>
<body>

    <div>@Model.Dag</div>
    <div>@Model.Maand</div>
    <div>@Model.Jaar</div>
</body>
</html>

VerwerkController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
    public class VerwerkController : Controller
    {
        // GET: Verwerk
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Verwerk(int dag, String maand, int jaar)
        {
            var model = new VerwerkModel
            {
                Dag = dag,
                Maand = maand,
                Jaar = jaar,
            };

            return View(model);
        }
    }
}

Index.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <form name="input" action="/Home/Verwerk" method="get" >
            Dag: <input type="number" name="dag" /><br />            
            Maand: <input type="text" name="maand" /><br />
            Jaar: <input type="number" name="jaar" /><br />
            <input type="submit" name="send" /><br />
        </form>
    </div>

</body>
</html>

当我按下发送按钮时出现错误。

  

De bron kan niet worden gevonden。 (无法找到来源)

     

Beschrijving(描述):HTTP 404. Mogelijk是de door u gezochte bron(een&gt; afhankelijkheid daarvan)verwijderd of tijdelijk niet beschikbaar,is de&na; naam ervan veranderd。 Controleer de spelling van de volgende URL。 (搜索的源可能会被删除或暂时不可用,或者其名称已更改。请检查以下URL是否有错误。)

     

Aangevraagde(请求)网址:/ Home / Verwerk

另一个错误:

>The view 'verwerk' or its master was not found or no view engine supports the >searched locations. The following locations were searched:
>~/Views/verwerk/verwerk.aspx
>~/Views/verwerk/verwerk.ascx
>~/Views/Shared/verwerk.aspx
>~/Views/Shared/verwerk.ascx
>~/Views/verwerk/verwerk.cshtml
>~/Views/verwerk/verwerk.vbhtml
>~/Views/Shared/verwerk.cshtml
>~/Views/Shared/verwerk.vbhtml

1 个答案:

答案 0 :(得分:0)

在这里,您似乎返回了一个不存在的视图:

return View(day + "/" + month + "/" + year);

我猜您没有名为07/03/2015.cshtml的视图,这是您尝试在此处呈现的视图。相反,您可以考虑渲染一些现有视图并传递正确的模型。例如,假设您已定义视图模型:

public class MyViewModel
{
    public int Dag { get; set; } 
    public string Maand { get; set; } 
    public int Jaar { get; set; }
}

和相应的视图Verwerk.cshtml

@model MyViewModel
<div>@Model.Dag</div>
<div>@Model.Maand</div>
<div>@Model.Jaar</div>

你可以渲染这个观点:

[HttpPost]
public ActionResult Verwerk(int dag, String maand, int jaar)
{
    var model = new MyViewModel
    {
        Dag = dag,
        Maand = maand,
        Jaar = jaar,
    };

    return View(model);
}

或者如果你想渲染像FooBar.cshtml这样的特定视图,你可以使用:

return View("FooBar", model);

您的表单操作也是错误的:

<form name="input" action="/home/verwerk" method="post">

应该是:

<form name="input" action="/verwerk/verwerk" method="post">

因为您的Verwerk操作似乎是在VerwerkController内而非HomeController内定义的。