我正在使用Visual Studio 2010和mvc 2.0。
在我的控制器中,我有一个 ViewResult 从视图页面customerDataAdd中挑选数据。有 8 个字段(文本框)。我有一个模型,只有这 8 个字段的 get{ } 和 set{ }。
问题是,在提交按钮的操作中,它转到 ViewResult,创建 model.Customer 的对象,尝试将数据从视图插入到模型属性并失败。
错误是:对象引用未设置为对象的实例。
下面是我的一些代码:
Customer.cs-模型类
private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;
public string LastName
{......}
private string _Street;
public string Street
{......}
private string _Village;
public string Village
{......}
private string _Thana;
public string Thana
{......}
private string _District;
public string District
{......}
private string _Division;
public string Division
{......}
private string _Country;
public string Country
{......}
CustomerController.cs -> ViewResul DisplayCustomer() -> 错误显示在第二行。
[HttpPost]
public ViewResult DisplayCustomer()
{
Models.Customer customerView = new Models.Customer(); //all customerview properties are null??
**customerView.FirstName = Request.Form["atxtFirstName"].ToString();** //firstname gets null??
customerView.LastName = Request.Form["atxtLastName"].ToString();
customerView.Street = Request.Form["atxtStreet"].ToString();
customerView.Village = Request.Form["atxtVillage"].ToString();
customerView.Thana = Request.Form["atxtThana"].ToString();
customerView.District = Request.Form["atxtDistrict"].ToString();
customerView.Division = Request.Form["atxtDivision"].ToString();
customerView.Country = Request.Form["atxtCountry"].ToString();
return View();
}
我的表单的 [edit-1] 值["atxtFirstName"].ToString() = null显示。如前所述,我已使用FormCollection对其进行了编辑。
[编辑-2]
这是我的DisplayCustomer.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SimpleExample.Models.Customer>" %>
显示客户
<h2>DisplayCustomer</h2>
<div>
<table runat="server" width="30%">
<tr>
<td width="30%">Customer Name: </td>
<td><%= Model.FirstName + " " + Model.LastName %></td>
</tr>
<tr>
<td>Customer Address: </td>
<td><%= Model.Street + ", " + Model.Village + ", " + Model.Thana + ", <br/>" + Model.District + ", " + Model.Division + ", " + Model.Country %></td>
</tr>
</table>
</div>
我的CustomerDataAdd.aspx - 这里只给出了表格。
<form id="afrmCustomer" runat="server" action="DisplayCustomer" method="post">
<table width="30%">
<tr>
<td width="30%">
<asp:Label ID="Label8" Text="First Name:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtFirstName" runat="server" />
</td>
<td width="30%">
<asp:Label ID="Label1" Text="Last Name:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtLastName" runat="server" />
</td>
</tr>
<tr>
<td width="30%">
<asp:Label ID="Label2" Text="Street:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtStreet" runat="server" />
</td>
<td width="30%">
<asp:Label ID="Label7" Text="Village:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtVillage" runat="server" />
</td>
</tr>
<tr>
<td width="30%">
<asp:Label ID="Label3" Text="Thana / Upazilla:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtThana" runat="server" />
</td>
<td width="30%">
<asp:Label ID="Label6" Text="District:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtDistrict" runat="server" />
</td>
</tr>
<tr>
<td width="30%">
<asp:Label ID="Label4" Text="Division:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtDivision" runat="server" />
</td>
<td width="30%">
<asp:Label ID="Label5" Text="Country:" runat="server" />
</td>
<td>
<asp:TextBox name="atxtCountry" runat="server" />
</td>
</tr>
<tr>
<td width="30%">
</td>
<td>
<asp:Button name="abtnSubmit" Text="Submit" runat="server" />
</td>
<td width="30%">
</td>
<td>
</td>
</tr>
</table>
</form>