Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 466.4k

The parameters dictionary contains a null entry for parameter 'lineNum

Jun 7 2023 10:29 AM

Hi Team

I have a controller calls a View, when i try to launch this View it reads a null property from the controller. How do i fix this problem? I have removed at sign as they represent a link i dont have enough reputation for hyperlinks.

//model
public class OrderLine
{
    
    public int OrderId { get; set; }
    public int LineNumber { get; set; }
    public string ProductCode { get; set; }
    public string ProductType { get; set; }
    public decimal CostPrice { get; set; }
    public decimal SalesPrice { get; set; }
    public int Quantity { get; set; }
    public List<OrderLine> OrderLines { get; set; }
}


// controller
//  OrderLine/Edit/{LineNumber}
public ActionResult Edit(int  lineNumber)
{

    OrderLine orderLine = GetOrderLineByLineNumber(lineNumber);

    if (orderLine == null)
    {
        return ttpNotFound();
    }

    return View(orderLine);
}

// Helper method to retrieve an order line by line number
private OrderLine GetOrderLineByLineNumber(int lineNumber)
{
    string connectionString = "Data Source=DESKTOP-C5QD55P\\SQLEXPRESS;Initial Catalog=SalesOrder;Integrated Security=True";

    OrderLine orderLine = null;
    using(SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        string query = "SELECT * FROM OrderLine WHERE LineNumber = LineNumber";
        SqlCommand command = new SqlCommand(query, connection);
        command.Parameters.AddWithValue("LineNumber", lineNumber);

        SqlDataReader reader = command.ExecuteReader();
        if(reader.Read())
        {
            orderLine = new OrderLine
            {
                LineNumber = (int)reader["LineNumber"],
                ProductCode = reader["ProductCode"].ToString(),
                ProductType = reader["ProductType"].ToString(),
                CostPrice = (decimal)reader["CostPrice"],
                SalesPrice = (decimal) reader["SalesPrice"],
                Quantity = (int) reader["Quantity"]
            };
        }
        reader.Close();
    }

    return orderLine; // Return the retrieved order line or null if not found
}
// view
model OrderApplicationXML.Models.OrderLine

using (Html.BeginForm("Edit", "OrderHeader", new { lineNumber = Model.LineNumber }, FormMethod.Post))

{
    Html.HiddenFor(model => model.OrderId)

    <div class="form-group">
        Html.LabelFor(model => model.ProductCode)
        Html.TextBoxFor(model => model.ProductCode, new { class = "form-control" })
    </div>

    <div class="form-group">
        Html.LabelFor(model => model.ProductType)
        Html.TextBoxFor(model => model.ProductType, new { class = "form-control" })
    </div>

    <div class="form-group">
        Html.LabelFor(model => model.CostPrice)
        Html.TextBoxFor(model => model.CostPrice, new { class = "form-control" })
    </div>

    <div class="form-group">
        Html.LabelFor(model => model.SalesPrice)
        Html.TextBoxFor(model => model.SalesPrice, new { class = "form-control" })
    </div>

    <div class="form-group">
        Html.LabelFor(model => model.Quantity)
        Html.TextBoxFor(model => model.Quantity, new { class = "form-control" })
    </div>

    <button type="submit" class="btn btn-primary">Update</button>
}

 


Answers (4)