用数组中的一组int保存到一个表

时间:2018-08-29 15:34:17

标签: c# asp.net-mvc

我正在尝试以这种格式保存到表中-

Table

我从复选框值中获取数据作为ViewModel中的EmbellishmentPositionID int数组,我想将它们保存到表中的DB(SQL)中。我也从VM获得EmbellishmentProductTypeID。

这一切都在MVC C#中

有人可以帮我吗?我已经尝试-

[HttpPost]
public ActionResult Edit(ProductTypeVM viewModel)
{
  try
  {
    if (viewModel == null)
      throw new ArgumentNullException(nameof(viewModel));

    if (ModelState.IsValid)
    {
      var dataModel = _epdRepository.Table.Where(x => x.EmbellishmentProductTypeID == viewModel.Id);

      _epdRepository.Delete(dataModel);


      var model = new EmbellishmentProductDetailRecord();

      model.EmbellishmentProductTypeID = viewModel.Id;

      _epdRepository.Insert(model);
    }

    else
    {
      throw new ArgumentNullException(nameof(viewModel));

    }
  }
  catch (Exception ex)
  {
    throw;
  }

  return View();
}

阵列图片-

Array Pic

1 个答案:

答案 0 :(得分:1)

这是一个示例。希望对您有帮助,我的朋友:))

foreach(var item in viewModel.Values) //loop all array data
{
    var model = new EmbellishmentProductDetailRecord();
      model.EmbellishmentProductTypeID = viewModel.Id;
      model.EmbellishmentPositionID = item;
      model.EmbellishmentID = ??? //I dont know which value to bind
      _epdRepository.Insert(model);
}