我试图生成我的第一个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);
}
}
}
好的开始,但我们需要更多的信息给你。 你在Get或Post中得到错误吗?发生异常的线是什么? – 2010-07-30 10:10:50
@ nprosser:其实小森说得很好。根据您提供的信息,我们无法确定这是否发生在创建过程的GET或POST中?当你想要显示这个视图时,或者在填充文本框并将其提交回服务器之后,会发生这种情况吗? – 2010-07-30 11:10:03