Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I used this code to get weight Value:

$("#productId").change(function () {
             var v = $(this).val();
           var weight = document.getElementById('weight');
           $.getJSON("/SalesItemLine/GetProductWeight?id=" + v, function (data) {
               console.log(data);
              $("#weight").val(data);


           });



Value appears in TextBox after I call it, But when I submit the form it is not saved and I got validation error.

What I have tried:

Many changing in codes using innerText, InnerHtml...etc
Posted
Comments
Chris Copeland 28-Nov-22 4:15am    
You've provided code for the getting part of the process but you've not provided any code for the submission part. Also I got validation error isn't very specific, try and provide more details. Did the validation error occur in the backend, the frontend, and what exactly did this validation error say?
Majid Farhan 28-Nov-22 7:39am    
Sorry for that.
validation error: {"salesProductWeight":["The salesProductWeight field is required."]}

and this is the post code:


// POST: api/SalesItemLine
[HttpPost]
[Authorize]
public async Task<iactionresult> PostSalesItemLine([FromBody] SalesItemLine salesItemLine)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

SalesItem salesItem = await _context.SalesItem
.Where(x => x.Id.Equals(salesItemLine.SalesItemId)).FirstOrDefaultAsync();


salesItemLine.TotalAmount = (decimal)salesItemLine.Qty * salesItemLine.Price;

if (salesItemLine.SalesItemLineId == string.Empty)
{
var weight = _context.SalesProduct.Where(c => c.Id == salesItemLine.SalesItemId).Select(c => c.SalesProductWeight).FirstOrDefault();
var price = _context.SalesProduct.Where(c => c.Id == salesItemLine.SalesItemId).Select(c => c.SalesProductPrice).FirstOrDefault();
salesItemLine.SalesProductWeight = weight;
salesItemLine.Price = price;
salesItemLine.SalesItemLineId = Guid.NewGuid().ToString();
_context.SalesItemLine.Add(salesItemLine);
await _context.SaveChangesAsync();
return Json(new { success = true, message = "Add new data success." });
}
else
{
_context.Update(salesItemLine);
await _context.SaveChangesAsync();
return Json(new { success = true, message = "Edit data success." });
}

}
Chris Copeland 28-Nov-22 9:43am    
The error is telling you quite clearly what the issue is. It's expecting a salesProductWeight value to be passed into the controller as part of the request, however it's not able to find it. You've provided the code for the controller but not the part for the Javascript, which is what is important here. You can use the Improve question button to amend your question with the relevant code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900