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 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)
Html.LabelFor(model => model.ProductCode)
Html.TextBoxFor(model => model.ProductCode, new { class = "form-control" })
Html.LabelFor(model => model.ProductType)
Html.TextBoxFor(model => model.ProductType, new { class = "form-control" })
Html.LabelFor(model => model.CostPrice)
Html.TextBoxFor(model => model.CostPrice, new { class = "form-control" })
Html.LabelFor(model => model.SalesPrice)
Html.TextBoxFor(model => model.SalesPrice, new { class = "form-control" })
Html.LabelFor(model => model.Quantity)
Html.TextBoxFor(model => model.Quantity, new { class = "form-control" })
}
Prasad RaveendranPosted Jun 7, 2023, 5:59 PM
I suspect the problem in passing the right Controlle in the below CSHTML Code
Please ensure whether its "OrderHeader" or "OrderLine"
Amit MohantyPosted Jun 7, 2023, 1:57 PM
In the code you provided, there is a problem with the SQL query in the
GetOrderLineByLineNumbermethod. The query is not correctly using the parameter forLineNumber. Here's the updated code:Guest UserPosted Jun 7, 2023, 1:49 PM
Hi Rajkiran
I made changes according to the answer, then i got this error "
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /OrderHeader/Edit
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /OrderHeader/Edit
Rajkiran SwainPosted Jun 7, 2023, 10:44 AM
Ensure that the controller name is correct ("OrderLine" in this case) so that the form is submitted to the correct action.
Once you make this change, the value of 'Model.LineNumber' will be passed to the Edit action in the OrderLine controller, and the 'orderLine' object will be correctly retrieved from the database based on the provided line number.