2012-12-21 33 views
2

我正在使用Visual Studio 2010mvc 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> 
+0

您应该在此处使用MVC约定 - 将DisplayCollection作为参数传递给DisplayCustomer方法 – levelnis

+1

您可以显示您的视图吗? –

+0

我的完整CustomerView类? –

回答

0

我得到了点。因为我正在使用服务器控件此过程不是从服务器控制传递值的正确方法。 我只需要使用html。但我不知道这是为什么。如果有人能回答,我将不胜感激。

我用:

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

,这适用于所有!

0

这样看来,价值是不是在你的形式发布

Request.Form["atxtFirstName"] 

您应该检查什么是张贴到这一行动。例如,在动作中放置一个断点,然后在浏览器或Visual Studio中查看后期对象,即您的选择。

任何你不会使用viewmodel作为参数的理由?

0

试试这个: -

[HttpPost] 

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

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

      return View(); 
     } 
+0

试过这个。仍然是同样的问题。 **你调用的对象是空的。** –

4

而不是使用服务器控件,你可以使用HTML帮助像这样的:

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

希望这会有所帮助。

相关问题