找不到资源MVC实体框架

时间:2017-02-04 17:27:22

标签: c# asp.net-mvc entity-framework

无法找到有效的答案,所以我再次提出这个问题。我正在使用带有MVC5和Entity Framework的Visual Studio来发送表单。我的问题是每当我尝试访问表单时出现错误,找不到资源' (它在我的屏幕上用法语,我希望这是正确的翻译)。 这是我的架构:

MonLivredor /控制器/ HomeController.cs

MonLivredor /视图/主页/ Formulaire.cshtml

以下是我在控制器中使用的方法,用于从表单中获取发布的数据:

    [HttpPost]
    public ActionResult Formulaire()
    {

        String nom = Request.Form["nom"];
        String mail = Request.Form["mail"];
        String message = Request.Form["message"];

        Commentaire com = new Commentaire(nom, mail, message);
        context.Liste.Add(com);
        context.SaveChanges();

        return View(context);

    }

最后我尝试在VS 2015中手动启动我的表格:

<h2>Formulaire</h2>
<form method="post" action="/Home/Formulaire">
    <h2>Nom</h2>
    <input type="text" id="nom" name="nom"/>
    <h2>Mail</h2>
    <input type="text" id="mail" name="mail"/>
    <h2>Message</h2>
    <input type="text" id="message" name="message"/>
</form>

以防万一可以提供帮助,这是我的global.asax.cs,没有任何修改:

namespace MonLivredor
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

这里是模型Commentaire.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace MonLivredor.Models
{
    public class Commentaire 
    {
        [Key]
        public int ID { get; set; }
        public String Nom { get; set; }
        public String Mail { get; set; }
        public String Message { get; set; }
        public DateTime CreateDate { get; set; }

        public Commentaire(String nom, String mail, String message)
        {
            CreateDate = DateTime.Now;
            this.Nom = nom;
            this.Mail = mail;
            this.Message = message;
        }

        public Commentaire()
        {
        }


    }
}

对于多余的主题感到抱歉,我找不到任何有用的东西。我试过改变我的路由,但它似乎也没有帮助。 谢谢你的帮助:)

1 个答案:

答案 0 :(得分:0)

您应该在表单中使用标记名称=“”,如下所示:

<h2>Formulaire</h2>
<form method="post" action="/Home/Formulaire">
    <h2>Nom</h2>
    <input type="text" id="nom" name="nom"/>
    <h2>Mail</h2>
    <input type="text" id="mail" name="mail"/>
    <h2>Message</h2>
    <input type="text" id="message" name="message"/>
</form>

更好的方法是使用ModelBinder:

[HttpPost]
    public ActionResult Formulaire(Commentaire com)
    {

        context.Liste.Add(com);
        context.SaveChanges();

        return View(context);

    }
表单的

和mvc框架辅助方法:

    <h2>Formulaire</h2>
    @using (Html.Beginform("Formulaire","Home"))
{ 
        <h2>Nom</h2>
        @Html.Editor("nom")
        <h2>Mail</h2>
        @Html.Editor("mail")
        <h2>Message</h2>
        @Html.Editor("message")

}