Results 1 to 3 of 3

Thread: HttpPost Action

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    HttpPost Action

    I'm new to MVC and this is just a learning exercise, the form submit executes the AddOrder action method where im expecting an object NewOrder passed from the view, but i get an error
    Object reference not set to an instance of an object.
    in the Post action method

    here are the controller methods
    Code:
     [HttpGet]
            public ActionResult AddOrder(string cuscode)
            {   
                AbmDbContext DB = new AbmDbContext();
                ViewBag.CusCode = cuscode;
    
                CUSTOMER  CUS = DB.CUSTOMERS.Single(x => x.CustomerCode == cuscode);
               //  var Prds = DB.PRODUCTS.Select(p => new { PrdID = p.UniqueID, PrdCode = p.ProductCode }).ToList();
                NewOrder NewOrd = new NewOrder(cus:CUS,RefNo:"XO-12345");
               
                return View(NewOrd); 
    
            }
    
             [HttpPost]
            public string AddOrder(NewOrder NewOrd)       // this not receiving the object from the view 
            {
                return NewOrd.customer.CustomerCode;
    
            }
    NewOrder class
    Code:
    public class NewOrder
        {
            public CUSTOMER customer { get; set; }
            public List<Tdtl> Lines { get; set; }
            public Thdr Header { get; set; }
    
            public NewOrder(CUSTOMER cus, string RefNo)
            {
                this.customer = cus;
                this.Header = new Thdr();
                this.Header.AccRef =RefNo;  
              
                // here auto generate the items into the list temporary 
                this.Lines = new List<Tdtl>();
                AbmDbContext DB = new AbmDbContext();
                var q = DB.PRODUCTS.Where(x => x.ProductTitle.Substring(0, 3) == "AXS").ToList();
                foreach (PRODUCT p in q )
                {
                    Lines.Add(new Tdtl() {ID=p.UniqueID ,Code=p.ProductCode,Desc=p.ProductTitle,UnitPrice=p.Price11,Qty=2,LineTotal=2*p.Price11 });
                }
            }
            public NewOrder()
            {
            }
    
            public bool Process()
            {
                return true;
            }
    
    
        }
    Thdr which is the order header
    Code:
     
     public class Thdr
        {
            [ScaffoldColumn(false)]
            public string ID { get; set; }
          [Display(Name="Customer")]
            public string CustomerCode{get;set;}
            [Display(Name = "Ref#")]
           public string  AccRef {get;set;}
            [Display(Name = "Due Date")]
            [DataType(DataType.Date)]
           public DateTime? DeliveryDate {get;set;}
            [Display(Name = "Amount")]
            [DataType(DataType.Currency )]
           public decimal  Total { get; set; }
        }
    Tdtl : order Lines

    Code:
     
    public class Tdtl
        {
            [ScaffoldColumn(false)]
            public string ID { get; set; }
    
            [Display(Name="Item Code")]
            public string Code { get; set; }
    
            [Display(Name = "Description")]
            public string Desc { get; set; }
    
            [Display(Name = "Quantity")]
            public decimal? Qty { get; set; }
    
            [DataType(DataType.Currency )]
            [Display(Name = "Unit Price")]
            public decimal? UnitPrice { get; set; }
    
             [DataType(DataType.Currency)]
             [Display(Name = "Total")]
            public decimal? LineTotal { get; set; }
        }
    and the View

    Code:
    @model AbmOrders.Models.NewOrder
    
    @{
        ViewBag.Title = "AddOrder";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h3>New Order For<span style="color :red"> @Model.customer.CustomerTitle </span></h3>
    
    
    
    
    @using (Html.BeginForm())
    { 
    
    <table>
        <tr>
           <td>
               <table>
                   <tr>
                       <td>
                           Customer Code  
                                               
                       </td>
                       <td>@Html.TextBox("CustomerCode",Model.customer.CustomerCode)</td>
                   </tr>
                   <tr>
                       <td> Order Number    </td>
                       <td>@Html.TextBox("AccRef",Model.Header.AccRef)</td>
                   </tr>
    
    
                
    
               </table>
    
           </td>
        </tr>
    
        <tr>
    
            <td>
    
    
    
    
       <table>
        <tr>
            <th>
               Code
            </th>
            <th>
               Desc
            </th>
            <th>
               Quantity
            </th>
            <th>
             Unit Price 
            </th>
            <th>
               Total
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model.Lines ) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Code)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Desc)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Qty)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnitPrice)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LineTotal)
            </td>
            <td>
    
    
    
        </tr>
    }
        </table>
        </td>
        
    </table>
    <p>
    <input style="text-align:right" type="submit" value="Save Order" />
    </p>    
    
    
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: HttpPost Action

    As always when an exception is thrown, please point out exactly what line it is thrown on and, in the case of a NullReferenceException, please point out which reference on that line is null.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: HttpPost Action

    error in the [HttpPost] AddOrder action, i only have one line to test the returned object properties

    [HttpPost]
    public string AddOrder(NewOrder NewOrd) // this not receiving the object from the view
    {
    return NewOrd.customer.CustomerCode;

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width