I am using Visual Studio 2010 and mvc 2.0.
In my controler, i have a ViewResult which is picking data from a view page customerDataAdd. there are 8 fields(textbox). I have a model just with get{ } and set{ } of this 8 field.
The problem is, in action of the submit button, it goes to ViewResult, create an object of the model.Customer, tries to insert data from the view to the models property and fail.
The error is: Object reference not set to an instance of an object.
Here is some of my code given below:
Customer.cs- model class
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() --> where the error shows is in second line.
[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] value of my form["atxtFirstName"].ToString() = null showing. I have edited it with FormCollection as said.
[edit-2]
Here is my 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>
My CustomerDataAdd.aspx - just the form is given here.
<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>
FormCollectionin as a parameter to your DisplayCustomer method