2010-07-30 40 views
4

我试图生成我的第一个MVC应用程序。我有一个非常基本的表格:团队:ID,姓名。我创建了MVC应用程序,并列出了表格。以下是创建视图。当它运行时,我收到消息:一个值是必需的。你能帮忙吗(对不起,这是非常基本的)。我的第一个创建页面与ASP.Net MVC2

查看create.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  Inherits="System.Web.Mvc.ViewPage<GettingStarted.Models.Team>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
Create 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>Create</h2> 

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %> 

<% using (Html.BeginForm()) {%> 

    <fieldset> 
     <legend>Fields</legend> 
     <p> 
      <label for="Name">Name:</label> 
      <%= Html.TextBox("Name") %> 
      <%= Html.ValidationMessage("Name", "*") %> 
     </p> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 

<% } %> 

<div> 
    <%=Html.ActionLink("Back to List", "Index") %> 
</div> 

</asp:Content> 

与控制器teamcontroller:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Ajax; 
using GettingStarted.Models; 
using DB = GettingStarted.Models.GettingStartedDataContext; 

namespace GettingStarted.Controllers 
{ 
    public class TeamController : Controller 
    { 
     // other actions 
     ... 
     // 
     // GET: /Team/Create 

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

     // 
     // POST: /Team/Create 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Create(Team team) 
     { 

      if (ModelState.IsValid) 
      { 
       try 
       { 
        var db = new DB(); 
        db.Teams.InsertOnSubmit(team); 
        db.SubmitChanges(); 
        return RedirectToAction("Index"); 
       } 
       catch 
       { 
        return View(team); 
       } 
      } 
      return View(team); 
     } 

    } 
} 
+1

好的开始,但我们需要更多的信息给你。 你在Get或Post中得到错误吗?发生异常的线是什么? – 2010-07-30 10:10:50

+0

@ nprosser:其实小森说得很好。根据您提供的信息,我们无法确定这是否发生在创建过程的GET或POST中?当你想要显示这个视图时,或者在填充文本框并将其提交回服务器之后,会发生这种情况吗? – 2010-07-30 11:10:03

回答

2

你创建的看法是强类型,以便提供一个视图模型实例:

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

public ActionResult Create() 
{ 
    return View((Team)null); 
} 
+0

感谢您的回复。传递模型实例是有意义的。然而,它仍然抱怨“创建不成功 - 一个值是必需的” – user406595 2010-07-30 10:32:31

+0

我被告知使用后(MS视频) 的[AcceptVerbs(HttpVerbs.Post) 公众的ActionResult创建(队队) { 如果(的ModelState .IsValid) { 尝试 { var db = new DB(); db.Teams.InsertOnSubmit(team); db.SubmitChanges(); return RedirectToAction(“Index”); } catch { return View(team); } } return View(team); } – user406595 2010-07-30 11:07:31

+0

@nprosser:你还可以改变你的问题中的代码与你用来获得所提到的异常的代码**和**附加该特定错误的堆栈跟踪,所以我们实际上可以看到发生了什么。 – 2010-07-30 11:07:42

0

提示:还要添加一个创建操作,它将Team作为参数来处理验证错误。

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

也不需要将空值传递给创建表单!你的问题可能在别的地方。你可以尝试使用的

<%= Html.TextBoxFor(model => model.Name) %> 
<%= Html.ValidationMessageFor(model => model.Name) %> 

代替

<%= Html.TextBox("Name") %> 
<%= Html.ValidationMessage("Name", "*") %> 

+0

这已经是控制器的一部分: 公众的ActionResult创建(队队) { 如果(ModelState.IsValid) { 尝试 {VAR DB =新的DB(); db.Teams.InsertOnSubmit(team); db.SubmitChanges(); return RedirectToAction(“Index”); } catch { return View(team); } } return View(team); } – user406595 2010-07-30 10:36:09

+1

更新我的回答,试试这个:) – 2010-07-30 10:41:30

+0

当我用'<%= Html.TextboxFor(model => model.Name)替换%> <%= Html.ValidationMessageFor(model => model.Name)%> '我收到消息 CS1061:'System.Web.Mvc.HtmlHelper '没有包含“TextboxFor”的定义,也没有包含接受类型为“System”的第一个参数的扩展方法“TextboxFor”。可以找到Web.Mvc.HtmlHelper '(你是否缺少使用指令或程序集引用?) – user406595 2010-07-30 10:48:29

1

该问题可能是模型中字段的注释。你有没有检查过你的模型:

public class Team { 

    [Required(ErrorMessage = "A value is required")] 
    public string whatEver {get; set;} 

    ... 
}