Reputation: 317
I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.
In a view, I have a button 'Enregistrer' which I add in it a JQuery Script.
The script is used to delete the item selected from the DropDownList 'Poste'.
This button has as purpose to save some values in a list named 'ListG'.
The problem that the button works perfectly but when i add the Script, the situation change and the button didn't works and became static (dead).
Scenario of my issue :
form is submitted to the action
action does something
The problem is that I must remove the item at the action.
Precisly :
I have 2 collections: PostesItems and ListG I must remove from PostesItems postes that are already on ListG.
This is the code in the view :
<%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.FlowViewModel>" %>
<% using (Html.BeginForm("Save", "ProfileGa")) { %>
<%: Html.ValidationSummary(true) %>
<fieldset class="parametrage">
<legend>Gestion de Gamme</legend>
<div>
<%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems, new { @id = "poste" })%>
<div>
<%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(model=>model.Nbr_Passage)%>
</div>
<div class="editor-field">
<%: Html.ValidationMessageFor(model => model.Nbr_Passage)%>
</div>
<div>
<%:Html.Label("Position :")%><%: Html.EditorFor(model=>model.Position)%>
</div>
<div>
<%:Html.Label("Poste Suivant :")%><%: Html.DropDownListFor(model => model.PosteSuivantSelected, Model.PostesItems, new { @id = "dd" })%>
</div>
<div>
<input type="submit" value="Enregistrer" id="btnSave" />
</div>
<script type="text/javascript">
$(function () {
$('#btnSave').click(function () {
$('#poste option:selected').remove();
});
});
</script>
and this is the code of model :
public class FlowViewModel
{
[Key]
public string IDv { get; set; }
[NotMapped]
public SelectList PostesItems { get; set; }
public List<Gamme> GaItems { get; set; }
public string SelectedPoste { get; set; }
public string PostePrecedentSelected { get; set; }
public string PosteSuivantSelected { get; set; }
public string Position { get; set; }
public string Nbr_Passage { get; set; }
[NotMapped]
public List<Gamme> ListG {get;set;}
}
And finally the code in the controller :
[HttpGet]
public ActionResult Index()
{
var viewModel = new FlowViewModel();
viewModel.PostesItems = new SelectList(db.Postes.ToList(), "ID_Poste", "ID_Poste");
viewModel.Profile_GaItems = db.Profil_Gas.ToList();
viewModel.GaItems = db.Gammes.ToList();
viewModel.ListG = (List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"];
return View(viewModel);
}
[HttpPost]
public ActionResult Save(FlowViewModel model)
{
if (ModelState.IsValid)
{
Gamme G = new Gamme();
G.ID_Gamme = model.SelectedProfile_Ga;
G.ID_Poste = model.SelectedPoste;
G.Next_Posts = model.PosteSuivantSelected;
G.Nbr_Passage = int.Parse(model.Nbr_Passage);
G.Position = int.Parse(model.Position);
((List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"]).Add(G);
var list = ((List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"]);
}
return RedirectToAction("Index");
}
Solution that I try :
I add this line to the methode Index in the controller :
viewModel.PostesItems.Where(x => !viewModel.ListG.Contains(x)).ToList();
But there is an error of compilation !
Upvotes: 2
Views: 187
Reputation: 7854
you are using linq against totally different objects
PostesItems is of type 'SelectList'
and ListG is of type List<Gamme>
How will you be able to equate them in the linq query.
Instead you should be using something like this
foreach(var postItem in viewModel.PostesItems){
viewModel.ListG.ForEach((x)=>{
if((x.PostId != postItem.Text) || (x.PostId != postItem.Value)){
// Your logic goes here...
}
});
}
Provide more details on what you wanted to accomplish in the line that gives you the error. You are trying to check if an object list contains a selectlistitem.
Upvotes: 1