Hi Team
I have a controller to edit a value from the form, but i keep getting this error on my controller that my property is nullable to int. How do i fix this issue? I try to put int? property name to fix this
What I have tried:
&
lt;pre>// POST: OrderHeader/Edit
[HttpPost]
public ActionResult Edit(OrderLine orderLine)
{
if (ModelState.IsValid)
{
// Update the order line in the database
UpdateOrderLine(orderLine);
return RedirectToAction("Index");
}
return View(orderLine);
}
private void UpdateOrderLine(OrderLine orderLine)
{
string connectionString = "Data Source=DESKTOP-C5QD55P\\SQLEXPRESS;Initial Catalog=SalesOrder;Integrated Security=True";
using(SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "UPDATE OrderLine SET ProductCode = @ProductCode, ProductType = @ProductType, CostPrice = @CostPrice, SalesPrice = @SalesPrice, Quantity = @Quantity WHERE LineNumber = @LineNumber";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@LineNumber", orderLine.LineNumber);
command.Parameters.AddWithValue("@ProductCode", orderLine.ProductCode);
command.Parameters.AddWithValue("@ProductType", orderLine.ProductType);
command.Parameters.AddWithValue("@CostPrice", orderLine.CostPrice);
command.Parameters.AddWithValue("@SalesPrice", orderLine.SalesPrice);
command.Parameters.AddWithValue("@Quantity", orderLine.Quantity);
command.ExecuteNonQuery();
}
}
// 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
}
// 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; }
}
// view for edit
@model OrderApplicationXML.Models.OrderLine
@using (Html.BeginForm("Edit", "OrderHeader", FormMethod.Post))
{
@Html.HiddenFor(model => model.LineNumber)
<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>
}
// routing config
routes.MapRoute(
name: "OrderHeaderEdit",
url: "OrderHeader/Edit/{LineNumber}",
defaults: new { controller = "OrderHeader", action = "Edit" }
);