I have built an MVC application which includes CRUD methods for a user to update some data. The controller for the update UI makes an HTTP call to another MVC controller, which is in my microservices application. My problem is that some of the data coming into the microservice controller has null properties.
I am trying to update the product details. I click on the button on the product and it redirects me to the product details of this product. The data for this product is transferred correctly, I am trying to update the product details but it does not work
I am trying to update product details for the product but the updating process is not working.
When I click the button to add data, the data is not added.
There is no problem in receiving data with HttpGetWhen I click on the button I get an issue like this
I am trying to update the product information, but the ProductID value in HttpPost is null and the update process is not performed.Also the process works via API
// Entity
public class ProductDetail
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string ProductDetailID { get; set; }
public string ProductDescription { get; set; }
public string ProductInformation { get; set; }
public string ProductID { get; set; }
[BsonIgnore]
public Product Product { get; set; }
}
// ProductDetail DTO
public class UpdateProductDetailDto
{
public string ProductDetailID { get; set; }
public string ProductDescription { get; set; }
public string ProductInformation { get; set; }
public string ProductID { get; set; }
}
//ProductDetail Service Interface
public interface IProductDetailService
{
Task<List<ResultProductDetailDto>> GetAllProductDetailAsync();
Task CreateProductDetailAsync(CreateProductDetailDto createProductDetailDto);
Task UpdateProductDetailAsync(UpdateProductDetailDto updateProductDetailDto);
Task DeleteProductDetailAsync(string id);
Task<GetByIdProductDetailDto> GetByIdProductDetailAsync(string id);
Task<GetByIdProductDetailDto> GetByProductIdProductDetailAsync(string id);
}
//ProductDetail Service
public class ProductDetailService : IProductDetailService
{
private readonly IMongoCollection<ProductDetail> _productDetailCollection;
private readonly IMapper _mapper;
public ProductDetailService(IMapper mapper, IDatabaseSettings _databaseSettings)
{
var client = new MongoClient(_databaseSettings.ConnectionString);
var database = client.GetDatabase(_databaseSettings.DatabaseName);
_productDetailCollection = database.GetCollection<ProductDetail>(_databaseSettings.ProductDetailCollectionName);
_mapper = mapper;
}
public async Task<GetByIdProductDetailDto> GetByProductIdProductDetailAsync(string id)
{
var values = await _productDetailCollection.Find<ProductDetail>(x => x.ProductID == id).FirstOrDefaultAsync();
return _mapper.Map<GetByIdProductDetailDto>(values);
}
public async Task UpdateProductDetailAsync(UpdateProductDetailDto updateProductDetailDto)
{
var values = _mapper.Map<ProductDetail>(updateProductDetailDto);
await _productDetailCollection.FindOneAndReplaceAsync(x => x.ProductDetailID == updateProductDetailDto.ProductDetailID, values);
}
}
// ProductDetail ApiController (Microservice)
[AllowAnonymous]
[Route("api/[controller]")]
[ApiController]
public class ProductDetailsController : ControllerBase
{
private readonly IProductDetailService _productDetailService;
public ProductDetailsController(IProductDetailService productDetailService)
{
_productDetailService = productDetailService;
}
[HttpGet("GetProductDetailByProductId")]
public async Task<IActionResult> GetProductDetailByProductId(string id)
{
var values = await _productDetailService.GetByProductIdProductDetailAsync(id);
return Ok(values);
}
[HttpPut]
public async Task<IActionResult> updateProductDetail(UpdateProductDetailDto updateProductDetailDto)
{
await _productDetailService.UpdateProductDetailAsync(updateProductDetailDto);
return Ok("Ürün Detayı Başarılıyla Güncellendi");
}
}
//ProductDetail Controller (WebUI)
[Area("Admin")]
[AllowAnonymous]
[Route("Admin/ProductDetail")]
public class ProductDetailController : Controller
{
private readonly IHttpClientFactory _httpClientFactory;
public ProductDetailController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
[Route("UpdateProductDetail/{id}")]
[HttpGet]
public async Task<IActionResult> UpdateProductDetail(string id)
{
var client = _httpClientFactory.CreateClient();
var responseMessage = await client.GetAsync("https://localhost:7070/api/ProductDetails/GetProductDetailByProductId?id=" + id);
if (responseMessage.IsSuccessStatusCode)
{
var jsonData = await responseMessage.Content.ReadAsStringAsync();
var values = JsonConvert.DeserializeObject<UpdateProductDetailDto>(jsonData);
return View(values);
}
return View();
}
[Route("UpdateProductDetail/{id}")]
[HttpPost]
public async Task<IActionResult> UpdateProductDetail(UpdateProductDetailDto updateProductDetailDto)
{
var client = _httpClientFactory.CreateClient();
var jsonData = JsonConvert.SerializeObject(updateProductDetailDto);
StringContent stringContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
var responseMessage = await client.PutAsync("https://localhost:7070/api/ProductDetails/", stringContent);
if (responseMessage.IsSuccessStatusCode)
{
return RedirectToAction("ProductListWithCategory", "Product", new { area = "Admin" });
}
return View();
}
}
Onur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.