开发者

Asp.net mvc, modifying SelectList

开发者 https://www.devze.com 2023-02-16 20:34 出处:网络
I\'m trying to write simple extension method for SelectList. API confuses me. public static SelectList Without(this SelectList selectList,int val){

I'm trying to write simple extension method for SelectList. API confuses me.

public static SelectList Without(this SelectList selectList,int val){
  //return new SelectList(selectList.Items.Where(x=>x.Value!=val)); <-----???
}
开发者_开发技巧

It should return new select list with same items w/o one which value matches argument val.


This works. Likely is a bit slow, but I don't care:

public static class SelectListExtensions{
  public static SelectList Without
   (this SelectList selectList, params int[] what){
    var items=selectList.Items.Cast<dynamic>()
      .Where(x=>!what.Any(z=>x.Value==z));
    return new SelectList(items);
  }
  public static SelectList Without<T>
   (this SelectList selectList,params T[] what) where T:Enumeration{
    var items=selectList.Items.Cast<dynamic>()
      .Where(x=>!what.Any(z=>x.Value==z.Value));
    return new SelectList(items);
  }
}

Better approaches are welcome.


Nope. That does not work. Rendered Html:

<select data-val="true" data-val-required="The JMC decision field is required." 
 id="JMCDecisionStatus" name="JMCDecisionStatus">
 <option>{ Name = Successful, Value = 2 }</option>
 <option>{ Name = Reserved, Value = 3 }</option>
 <option>{ Name = Rejected, Value = 4 }</option>
</select>

Not exactly what I was looking for. :D


Do you just need to apply a ToArray() to your IEnumerable collection??

return new SelectList(selectList.Items.Where(x=>x.Value!=val).ToArray());


return new SelectList(selectList.Items.Cast<T>().Except(what));
0

精彩评论

暂无评论...
验证码 换一张
取 消