ASP.NET MVC2 - 如何创建表单?

时间:2011-01-23 21:31:05

标签: asp.net-mvc-2

如何在ASP.NET MVC2中创建表单,将数据发送到向数据库添加内容然后重定向到主页的控制器?你能给我一个关于如何在视图中完成它的示例/片段吗?


出于某种原因,我的表单中有一个错误。这是代码:

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;
    }

我在表单中输入详细信息,我得到以下例外:

  

此属性不能设置为空值。

<{1>} event_location。任何人都可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

asp.net/mvc网站包含大量有关MVC的示例,视频和教程,值得一读。以下是您可以实施的方案如何实施的示例:

型号:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

控制器:

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部分。通常我会创建一个存储库来将我的数据访问逻辑与控制器分离:

public interface IPersonsRepository
{
    void Save(Person person);
}

然后使用构建函数将此存储库注入我的控制器:

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, ...

一旦做出选择,您只需实现界面并指示您的DI框架将正确的实现传递给控制器​​构造函数。