如何在 ASP.NET MVC2 中创建表单,将数据发送到向数据库添加内容的 Controller ,然后重定向到主页?您能给我一个如何在 View 中完成的示例/片段吗?
由于某种原因,我的表单中有一个错误。代码如下:
AddEvent.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Add Event</h2>
<% using (Html.BeginForm()) { %>
<div>
<%= Html.LabelFor(x => x.EventName) %>:
<%= Html.TextBoxFor(x => x.EventName) %>
</div>
<div>
<%= Html.LabelFor(x => x.EventDate) %>:
<%= Html.TextBoxFor(x => x.EventDate) %>
</div>
<div>
<%= Html.LabelFor(x => x.EventLocation) %>:
<%= Html.TextBoxFor(x => x.EventLocation) %>
</div>
<div>
<%= Html.LabelFor(x => x.EventDescription) %>: </br>
<%= Html.TextAreaFor(x => x.EventDescription) %>
</div>
<input type="submit" value="Submit" />
<% } %>
HomeController.cs
public ActionResult AddEvent()
{
return View();
}
[HttpPost]
public ActionResult AddEvent(Event e)
{
e.EventCreatorName = Session["UserName"].ToString();
DatabaseModels db = new DatabaseModels();
db.AddEvent(e);
return RedirectToAction("Index", "Home");
}
DatabaseModels.cs
public bool AddEvent(Event e)
{
anEvent eventToAdd = new anEvent();
eventToAdd.creator_nickname = e.EventCreatorName;
eventToAdd.event_category = 1; // TODO
if (e.EventDate == null)
{
eventToAdd.event_date = new DateTime();
}
else
{
eventToAdd.event_date = DateTime.Parse(e.EventDate);
}
eventToAdd.event_location = e.EventLocation;
eventToAdd.event_name = e.EventName;
m_db.AddToevents(eventToAdd);
m_db.SaveChanges();
return true;
}
我在表单中输入详细信息,但出现以下异常:
This property cannot be set to a null value.
在event_location
上。谁能帮忙解决这个问题吗?
最佳答案
asp.net/mvc该网站包含大量值得阅读的有关 MVC 的示例、视频和教程。以下是如何实现您所询问的场景的示例:
型号:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Controller :
public class PersonsController: Controller
{
public ActionResult Index()
{
return View(new Person());
}
[HttpPost]
public ActionResult Index(Person person)
{
// The person object here will have it's FirstName
// and LastName properties bound to whatever values
// the user entered in the corresponding textboxes in the form
// TODO: save person to database
// redirect to /home/index
return RedirectToAction("index", "home");
}
}
查看:
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.Person>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<div>
<%= Html.LabelFor(x => x.FirstName) %>:
<%= Html.TextBoxFor(x => x.FirstName) %>
</div>
<div>
<%= Html.LabelFor(x => x.LastName) %>:
<%= Html.TextBoxFor(x => x.LastName) %>
</div>
<input type="submit" value="Save" />
<% } %>
</asp:Content>
现在您可能想知道 TODO 部分。通常我会创建一个存储库来将数据访问逻辑与 Controller 分离:
public interface IPersonsRepository
{
void Save(Person person);
}
然后使用构造函数将此存储库注入(inject)到我的 Controller 中:
public class PersonsController: Controller
{
private readonly IPersonsRepository _repository;
public PersonsController(IPersonsRepository repository)
{
_repository = repository;
}
public ActionResult Index()
{
return View(new Person());
}
[HttpPost]
public ActionResult Index(Person person)
{
// The person object here will have it's FirstName
// and LastName properties bound to whatever values
// the user entered in the corresponding textboxes in the form
// save person to database
_repository.Save(person);
// redirect to /home/index
return RedirectToAction("index", "home");
}
}
显然现在剩下的最后一部分是这个存储库的实现。这将取决于您的数据存储方式/位置以及您将使用的特定数据访问技术。那么您是否使用关系数据库、平面文本文件、XML 文件、对象数据库、存储在云上的某些数据库……您将如何访问它们:EF、NHibernate、Linq-to-XML、一些 REST API、 ...
一旦做出选择,您只需实现接口(interface)并指示 DI 框架将正确的实现传递给 Controller 构造函数。
关于asp.net-mvc-2 - ASP.NET MVC2 - 如何创建表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4776653/