Saving to m-2 with flags

So, I'm trying to use [Slauma Answer] [1]

What have I done so far

ViewModelProspectUsers Model

public int Id { get; set; } public string User { get; set; } public IEnumerable<ViewModelUserProspectSelect> Prospects { get; set; } 

ViewModelUserProspectSelect Model

  public int ProspectID { get; set; } public string Name { get; set; } public bool IsSelected { get; set; } 

View UserInProspect

 @model OG.ModelView.ViewModelProspectUsers @using (Html.BeginForm()) { <div class="container"> <div class="contentContainer"> @Html.HiddenFor(model => model.Id) Prospects for User <h3>@Html.DisplayTextFor(model => model.User)</h3> </div> <div class="contentContainer2"> <h5>Please Select Prospects you wish to assign to this User.</h5> <p></p> @Html.EditorFor(model => model.Prospects) </div> <input type="submit" value="Save changes" /> @Html.ActionLink("Cancel", "Index") </div> } 

View Editor Located in ChangeUserInfo / EditorTemplates / * _ ViewModelUserprospectSelect.cshtml *

 @model OG.ModelView.ViewModelUserProspectSelect @Html.HiddenFor(model => model.ProspectID) @Html.HiddenFor(model => model.Name) test @Html.LabelFor(model => model.IsSelected, Model.Name) @Html.EditorFor(model => model.IsSelected) 

[Get] Method UserInProspect Action

 public ActionResult UsersInProspect(int id = 0) { var data = db.UserProfiles .Where(s => s.UserID == id) .Select(s => new { ViewModel = new ViewModelProspectUsers { Id = s.UserID, User = s.UserName }, prospects = s.Prospects.Select(c => c.ProspectID) }) .SingleOrDefault(); if (data == null) return HttpNotFound(); // Load all companies from the DB data.ViewModel.Prospects = db.Prospect .Select(c => new ViewModelUserProspectSelect { ProspectID = c.ProspectID, Name = c.ProspectName }) .ToList(); // Set IsSelected flag: true (= checkbox checked) if the company // is already related with the subscription; false, if not foreach (var c in data.ViewModel.Prospects) c.IsSelected = data.prospects.Contains(c.ProspectID); return View(data.ViewModel); } 

[HttpPost] UserInProspect Action Method

  public ActionResult UsersInProspect(ViewModelProspectUsers viewModel) { if (ModelState.IsValid) { var subscription = db.UserProfiles.Include(s => s.Prospects) .SingleOrDefault(s => s.UserID == viewModel.Id); if (subscription != null) { // Update scalar properties like "Amount" //subscription.Prospects = viewModel.Prospects; //subscription. = subscription. //List<string> myList = new List<string>(); //myList = viewModel.Prospects.Cast<String>().ToList(); //IEnumerable<dbProspect> Isubscription = subscription.Prospects; ////or explicit: //var iPersonList = (IEnumerable<dbProspect>)myList; // or more generic for multiple scalar properties // _context.Entry(subscription).CurrentValues.SetValues(viewModel); // But this will work only if you use the same key property name // in ViewModel and entity foreach (var prospect in viewModel.Prospects) { if (prospect.IsSelected) { if (!subscription.Prospects.Any( c => c.ProspectID == prospect.ProspectID)) { // if company is selected but not yet // related in DB, add relationship var addedProspect = new dbProspect { ProspectID = prospect.ProspectID }; db.Prospect.Attach(addedProspect); subscription.Prospects.Add(addedProspect); } } else { var removedProspect = subscription.Prospects .SingleOrDefault(c => c.ProspectID == prospect.ProspectID); if (removedProspect != null) // if company is not selected but currently // related in DB, remove relationship subscription.Prospects.Remove(removedProspect); } } db.SaveChanges(); } return RedirectToAction("Index"); } return View(viewModel); } 
+1
c # post checkbox asp.net-mvc many-to-many
source share
1 answer

Added a link to my controller using OG.Views.ChangeUsersInfo.EditorTemplates;

Remove it again. "EditorTemplates" should not be a namespace.

Added editor template under Views / ChangeUsers / Info / EditorTemplates / ViewModeluserProspectSelect.cs

An โ€œeditor templateโ€ is not a C # code file ( .cs ), but a Razor view ( .cshtml ). Move the ViewModelUserProspectSelect.cs file to the folder where ViewModelProspectUsers.cs also, and change its namespace to the same for both classes ( OG.Models ).

(Why is there a subfolder /Info/ ? Or is it a typo and only Views/ChangeUsersInfo/EditorTemplates ? I assume that the controller has the name ChangeUsersInfoController , right?)

Then create a new ViewModelUserProspectSelect.cshtml file in the Views/ChangeUsersInfo/EditorTemplates that contains the view from another answer, this:

 @model OG.Models.ViewModelUserProspectSelect @Html.HiddenFor(model => model.ProspectID) @Html.HiddenFor(model => model.Name) @Html.LabelFor(model => model.IsSelected, Model.Name) @Html.EditorFor(model => model.IsSelected) 

And the div element contentContainer2 div in your main view should look like this:

 <div class="contentContainer2"> <h5>Please Select Prospects you wish to assign to this User.</h5> @Html.EditorFor(model => model.Propects) </div> 
+2
source share

All Articles