【问题标题】:ASP.NET MVC2 - How to create a form?ASP.NET MVC2 - 如何创建表单?
【发布时间】:2011-01-23 21:31:04
【问题描述】:

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


由于某种原因,我的表单中有错误。代码如下:

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

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

此属性不能设置为空值。

event_location。谁能帮忙解决这个问题?

【问题讨论】:

  • 你检查过 e.EventLocation 的值是多少吗?根据您的描述,它似乎为空,而 event_location 不能为空。
  • 我已经尽可能地调试了它,据我所知,控制器没有得到任何插入到表单中的数据。
  • 好吧,显然我忘记了{get; set;}...它现在可以工作了。

标签: asp.net-mvc-2


【解决方案1】:

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 框架将正确的实现传递给控制器​​构造函数。

【讨论】:

  • 谢谢!不过,我确实有一个错误,请查看已编辑的问题。
猜你喜欢
  • 2012-05-11
  • 2011-02-11
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2011-01-03
相关资源
最近更新 更多