Skip to main content
deleted 1 characters in body
Source Link
Iridio
  • 9.3k
  • 4
  • 51
  • 71
[HttpPost]
public ActionResult MyForm(MyViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      //your mapping code or whatever...

      //You do your things with the selected ids..
      if (model.PriorInsuranceCompaniesSelected != null && model.PriorInsuranceCompaniesSelected.Count > 0)
        entity.PriorInsuranceCompanies = repository.GetSettoriByIdsGetCompaniesBy(model.PriorInsuranceCompaniesSelected);
      else
        entity.PriorInsuranceCompanies = new List<Comapny>();
      repository.Save(entity);

      return RedirectToAction("Index");
    }
    catch (RulesException ex)
    {
      ex.CopyTo(ModelState);
    }
    catch
    {
      ModelState.AddModelError("", "My generic error taken form a resource");
    }
  }
 
  //rehydratates the list in case of errors
  //....
  return View(model);
}
[HttpPost]
public ActionResult MyForm(MyViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      //your mapping code or whatever...

      //You do your things with the selected ids..
      if (model.PriorInsuranceCompaniesSelected != null && model.PriorInsuranceCompaniesSelected.Count > 0)
        entity.PriorInsuranceCompanies = repository.GetSettoriByIds(model.PriorInsuranceCompaniesSelected);
      else
        entity.PriorInsuranceCompanies = new List<Comapny>();
      repository.Save(entity);

      return RedirectToAction("Index");
    }
    catch (RulesException ex)
    {
      ex.CopyTo(ModelState);
    }
    catch
    {
      ModelState.AddModelError("", "My generic error taken form a resource");
    }
  }
 
  //rehydratates the list in case of errors
  //....
  return View(model);
}
[HttpPost]
public ActionResult MyForm(MyViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      //your mapping code or whatever...

      //You do your things with the selected ids..
      if (model.PriorInsuranceCompaniesSelected != null && model.PriorInsuranceCompaniesSelected.Count > 0)
        entity.PriorInsuranceCompanies = repository.GetCompaniesBy(model.PriorInsuranceCompaniesSelected);
      else
        entity.PriorInsuranceCompanies = new List<Comapny>();
      repository.Save(entity);

      return RedirectToAction("Index");
    }
    catch (RulesException ex)
    {
      ex.CopyTo(ModelState);
    }
    catch
    {
      ModelState.AddModelError("", "My generic error taken form a resource");
    }
  }
 
  //rehydratates the list in case of errors
  //....
  return View(model);
}
added 106 characters in body
Source Link
Iridio
  • 9.3k
  • 4
  • 51
  • 71
  model.PriorInsuranceCompaniesSelected = new List<int>();
  var companies = repository.GetPriorInsuranceCompanies();
  //add to your PriorInsuranceCompaniesSelected the values already checked from your entity
  var entity = repository.GetEntityBy(id);
  if (entity.PriorInsuranceCompanies != null)
    foreach (var item in entity.PriorInsuranceCompanies)
      model.PriorInsuranceCompaniesSelected.Add(item.Id);

  var select = (from s in companies select new { Id = s.Id, Name = s.Name }).OrderBy(x => x.Name); //.ToList;
  model.SettoriPriorInsuranceCompanies = new MultiSelectList(select, "Id", "Name", model.PriorInsuranceCompaniesSelected);

Then, onOn post, the ModelBinder will map the correct objects to your model automagically. youYou simply have to check values in model.PriorInsuranceCompaniesSelected

[HttpPost]
public ActionResult MyForm(MyViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      //your mapping code or whatever...

      //You do your things with the selected ids..
      if (model.PriorInsuranceCompaniesSelected != null && model.PriorInsuranceCompaniesSelected.Count > 0)
        entity.PriorInsuranceCompanies = repository.GetSettoriByIds(model.PriorInsuranceCompaniesSelected);
      else
        entity.PriorInsuranceCompanies = new List<Comapny>();
      repository.Save(entity);

      return RedirectToAction("Index");
    }
    catch (RulesException ex)
    {
      ex.CopyTo(ModelState);
    }
    catch
    {
      ModelState.AddModelError("", "My generic error taken form a resource");
    }
  }
 
  //rehydratates the list in case of errors
  //....
  return View(model);
}
  model.PriorInsuranceCompaniesSelected = new List<int>();
  var companies = repository.GetPriorInsuranceCompanies();
  //add to your PriorInsuranceCompaniesSelected the values already checked from your entity
  var entity = repository.GetEntityBy(id);
  if (entity.PriorInsuranceCompanies != null)
    foreach (var item in entity.PriorInsuranceCompanies)
      model.PriorInsuranceCompaniesSelected.Add(item.Id);

  var select = (from s in companies select new { Id = s.Id, Name = s.Name }).OrderBy(x => x.Name); //.ToList;
  model.Settori = new MultiSelectList(select, "Id", "Name", model.PriorInsuranceCompaniesSelected);

Then, on post, the ModelBinder will map the correct objects to your model automagically. you simply have to check values in model.PriorInsuranceCompaniesSelected

  model.PriorInsuranceCompaniesSelected = new List<int>();
  var companies = repository.GetPriorInsuranceCompanies();
  //add to your PriorInsuranceCompaniesSelected the values already checked from your entity
  var entity = repository.GetEntityBy(id);
  if (entity.PriorInsuranceCompanies != null)
    foreach (var item in entity.PriorInsuranceCompanies)
      model.PriorInsuranceCompaniesSelected.Add(item.Id);

  var select = (from s in companies select new { Id = s.Id, Name = s.Name }).OrderBy(x => x.Name); //.ToList;
  model.PriorInsuranceCompanies = new MultiSelectList(select, "Id", "Name", model.PriorInsuranceCompaniesSelected);

On post, the ModelBinder will map the correct objects to your model automagically. You simply have to check values in model.PriorInsuranceCompaniesSelected

[HttpPost]
public ActionResult MyForm(MyViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      //your mapping code or whatever...

      //You do your things with the selected ids..
      if (model.PriorInsuranceCompaniesSelected != null && model.PriorInsuranceCompaniesSelected.Count > 0)
        entity.PriorInsuranceCompanies = repository.GetSettoriByIds(model.PriorInsuranceCompaniesSelected);
      else
        entity.PriorInsuranceCompanies = new List<Comapny>();
      repository.Save(entity);

      return RedirectToAction("Index");
    }
    catch (RulesException ex)
    {
      ex.CopyTo(ModelState);
    }
    catch
    {
      ModelState.AddModelError("", "My generic error taken form a resource");
    }
  }
 
  //rehydratates the list in case of errors
  //....
  return View(model);
}
Post Undeleted by Iridio
added 106 characters in body
Source Link
Iridio
  • 9.3k
  • 4
  • 51
  • 71

The approach you are using is correct. I agree with you in using always viewmodels and never ViewBag. You

In your viewmodel you should change your dictionary to MultiSelectList so you can have The missing part is in your Html the selected values also.

public IList<int> PriorInsuranceCompaniesSelected { get; set; }
public MultiSelectList PriorInsuranceCompanies { get; set; }

You have to simply outputthen map the list of Checkboxes in afirst field if some Ids are already selected (info that you get when loading data from your repo for loop. Then, on post,example) and the ModelBinder will mapsecond with all the correct objects to your model automagicallyvalues.

Somthing similar to thisFrom your controller in the Get part (just some code as an example):

@foreach  model.PriorInsuranceCompaniesSelected = new List<int>();
  var checkcompanies = repository.GetPriorInsuranceCompanies();
  //add to your PriorInsuranceCompaniesSelected the values already checked from your entity
  var entity = repository.GetEntityBy(id);
  if (entity.PriorInsuranceCompanies != null)
    foreach (var item in Modelentity.InsuranceCompaniesPriorInsuranceCompanies)
      model.PriorInsuranceCompaniesSelected.Add(item.Id);

  var select = (from s in companies select new {
  Id <input= type='checkbox's.Id, name='InsuranceCompanies'Name value='@item= s.Key'>@itemName }).ValueOrderBy(x => x.Name); //.ToList;
  <inputmodel.Settori type='checkbox'= name='InsuranceCompanies'new value='@itemMultiSelectList(select, "Id", "Name", model.Key'>@itemPriorInsuranceCompaniesSelected);

Then in your Html you will have an output like this

@foreach (var item in Model.ValuePriorInsuranceCompanies)
{
   <label for="@item.Value" class="check">
   <input type='checkbox'type="checkbox" name='InsuranceCompanies'id="@item.Value" value='@itemname="PriorInsuranceCompaniesSelected" value="@item.Key'>@itemValue" @(item.ValueSelected ? "checked" : "") />@item.Text</label>
}

Then, on post, the ModelBinder will map the correct objects to your model automagically. you simply have to check values in model.PriorInsuranceCompaniesSelected

This should give you an idea of what to do. I hope it helps

The approach you are using is correct. I agree with you in using always viewmodels and never ViewBag. You should change your dictionary to MultiSelectList so you can have The missing part is in your Html.

You have to simply output the list of Checkboxes in a for loop. Then, on post, the ModelBinder will map the correct objects to your model automagically

Somthing similar to this

@foreach (var check in Model.InsuranceCompanies) {
   <input type='checkbox' name='InsuranceCompanies' value='@item.Key'>@item.Value
  <input type='checkbox' name='InsuranceCompanies' value='@item.Key'>@item.Value
  <input type='checkbox' name='InsuranceCompanies' value='@item.Key'>@item.Value
}

The approach you are using is correct. I agree with you in using always viewmodels and never ViewBag.

In your viewmodel you should change your dictionary to MultiSelectList so you can have the selected values also.

public IList<int> PriorInsuranceCompaniesSelected { get; set; }
public MultiSelectList PriorInsuranceCompanies { get; set; }

You then map the first field if some Ids are already selected (info that you get when loading data from your repo for example) and the second with all the values.

From your controller in the Get part (just some code as an example):

  model.PriorInsuranceCompaniesSelected = new List<int>();
  var companies = repository.GetPriorInsuranceCompanies();
  //add to your PriorInsuranceCompaniesSelected the values already checked from your entity
  var entity = repository.GetEntityBy(id);
  if (entity.PriorInsuranceCompanies != null)
    foreach (var item in entity.PriorInsuranceCompanies)
      model.PriorInsuranceCompaniesSelected.Add(item.Id);

  var select = (from s in companies select new { Id = s.Id, Name = s.Name }).OrderBy(x => x.Name); //.ToList;
  model.Settori = new MultiSelectList(select, "Id", "Name", model.PriorInsuranceCompaniesSelected);

Then in your Html you will have an output like this

@foreach (var item in Model.PriorInsuranceCompanies)
{
   <label for="@item.Value" class="check">
   <input type="checkbox" id="@item.Value" name="PriorInsuranceCompaniesSelected" value="@item.Value" @(item.Selected ? "checked" : "") />@item.Text</label>
}

Then, on post, the ModelBinder will map the correct objects to your model automagically. you simply have to check values in model.PriorInsuranceCompaniesSelected

This should give you an idea of what to do. I hope it helps

Post Deleted by Iridio
added 106 characters in body
Source Link
Iridio
  • 9.3k
  • 4
  • 51
  • 71
Loading
Source Link
Iridio
  • 9.3k
  • 4
  • 51
  • 71
Loading