2

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>
8
  • You should be using MVC conventions here - pass FormCollection in as a parameter to your DisplayCustomer method Commented Dec 21, 2012 at 9:29
  • my full CustomerView class? Commented Dec 21, 2012 at 9:48
  • added DisplayCustomer.aspx code. two of the line is not showing. I donno why. These lines are attached here: **<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> DisplayCustomer </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> ** Commented Dec 21, 2012 at 9:56
  • how do you submit values to the server? Commented Dec 21, 2012 at 9:58
  • in <form runat="server" action="DisplayCustomer"> Commented Dec 21, 2012 at 10:00

4 Answers 4

4

Instead of using server controls you can use html helper like this :

  <% Html.EditorFor(m=>m.FirstName) %>
  <% Html.EditorFor(m=>m.LastName) %>

hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

It would appear that value is not in your form posted

Request.Form["atxtFirstName"]

You should check what is posted to that action. For example, put a break point in the action and then look at the post object in your browser or in visual studio, your choice.

Any reason you would not use a viewmodel as a parameter to that action?

Comments

0

Try this:-

  [HttpPost]

        public ViewResult DisplayCustomer(FormCollection form)
        {
            Models.Customer customerView = new Models.Customer();  

            customerView.FirstName = form["atxtFirstName"].ToString();

            return View();
        }

1 Comment

Tried this. still the same problem. Object reference not set to an instance of an object.
0

I got the point. as i am using server control this process is not the correct one for passing value from server control. I just have to use html. But i don't know why this is. Please if anyone can answer, i will be grateful.

i used:

<input type="text" name="atxtFirstName" />

and this works for all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.