【问题标题】:viewing data in asp .net MVC - object reference not set在 asp .net MVC 中查看数据-未设置对象引用
【发布时间】:2012-12-21 09:25:44
【问题描述】:

我正在使用 Visual Studio 2010mvc 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();
    }

[编辑-1] 我的 form["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

<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 约定 - 将 FormCollection 作为参数传递给您的 DisplayCustomer 方法
  • 我的完整 CustomerView 课程?
  • 添加了 DisplayCustomer.aspx 代码。两条线没有显示。我不知道为什么。这些行附在此处:** DisplayCustomer **
  • 如何向服务器提交值?
  • in

标签: asp.net asp.net-mvc asp.net-mvc-2


【解决方案1】:

您可以像这样使用 html 助手,而不是使用服务器控件:

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

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您的表单中似乎没有发布该值

    Request.Form["atxtFirstName"]
    

    您应该检查发布到该操作的内容。例如,在动作中放置一个断点,然后在您的浏览器或 Visual Studio 中查看帖子对象,由您选择。

    您有什么理由不使用视图模型作为该操作的参数?

    【讨论】:

      【解决方案3】:

      试试这个:-

        [HttpPost]
      
              public ViewResult DisplayCustomer(FormCollection form)
              {
                  Models.Customer customerView = new Models.Customer();  
      
                  customerView.FirstName = form["atxtFirstName"].ToString();
      
                  return View();
              }
      

      【讨论】:

      • 试过这个。仍然是同样的问题。 对象引用未设置为对象的实例。
      【解决方案4】:

      我明白了。因为我正在使用 服务器控件,所以这个过程不是从服务器控件传递值的正确过程。 我只需要使用 html。但我不知道这是为什么。谁能解答,不胜感激。

      我用过:

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

      这对所有人都有效!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-24
        • 2013-01-19
        • 1970-01-01
        • 2016-07-06
        • 2015-08-28
        • 2013-07-26
        • 2021-01-16
        • 2014-10-15
        相关资源
        最近更新 更多