我正在使用Visual Studio 2010和mvc 2.0。在asp.net中查看数据MVC - 未设置对象引用
在我的控制器中,我有一个ViewResult,它从一个查看页面中挑选数据customerDataAdd。有8个字段(文本框)。我有一个模型,只需在此字段中获取{}并设置{}。
问题是,在提交按钮的操作中,它转到ViewResult,创建模型的一个对象。客户尝试从视图向模型属性插入数据并失败。
错误提示:未将对象引用设置为对象的实例。 - > ViewResul DisplayCustomer() - >其中的错误显示
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:
下面是一些我的代码如下在第二行。
[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();
}
[编辑-1] 我形式[ “atxtFirstName”]的值。的ToString() = 空展示。我已经用FormCollection进行了编辑。
[编辑-2]
这是我的DisplayCustomer.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SimpleExample.Models.Customer>" %>
DisplayCustomer
<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>
您应该在此处使用MVC约定 - 将DisplayCollection作为参数传递给DisplayCustomer方法 – levelnis
您可以显示您的视图吗? –
我的完整CustomerView类? –