Azureはじめました

Windows Azureで業務システムを組んでみる日記

jquery.validate.unobtrusive でクライアントサイドで動的に追加したフィールドをチェック

i am using jquery's unobtrusive validation plugin in with asp.net mvc. the problem is that all the fields that are rendered server side gets validated with this scheme but if i dynamically add a field in the form using javascript it is not validated despite adding html-5 data-* attributes to the field. can anyone guide me in right direction on how i can achieve this goal thanks

jquery - client side validation with dynamically added field - Stack Overflow
続きを読む

@Html.DropdownListがSelectedを消してしまう

しかも"たまに" (# ゚Д゚)ふぁーーっく!!!

こんな状況

//---model---
public class StayType{
  public int id{get;set;}
  public string name{get;set;}
}

public class Reserve{
   [いろいろ]
   public ICollection<ReserveItem> items{get;set;}
}

public class ReserveItem{
   [いろいろ]
   public int stayTypeId{get;set;}
}
  
//---controller---

public ActionResult Edit(int id){
   ViewBag.StayTypes = entity.StayType.ToList();
   return View();
}

//---View----

@foreach (item in Model.items){
<table> 
  <tr>
    <td>@Html.DropdownList("StayType",new SelectList(ViewBag.StayTypes,"id","name"item.StayTypeId),new{}) </td>
   ...

}

これで出力すると、StayTypeId=3にも関わらず

<select class="staytypeselect" name="StayType"><option value="1">1泊2食</option>
<option value="2">1泊3食</option>
<option value="4">1泊朝食</option>
<option value="3">1泊夕食</option>
<option value="5">素泊まり</option>
</select>

こうなる。(# ゚Д゚)

SelectListをダンプするとValue=3のSelectListItemがSelectedになってる。


(# ゚Д゚) ファック

続きを読む

Microsoft.Owin.Security.AuthenticationManagerでサインアウトができない

The issue is easily reproduced in the default asp.net project template by setting the validateInterval parameter of the OnValidateIdentity to 1 minute, and then waiting 1 minute before clicking the log off button. If the call to the action that calls SignOut happens to trigger the regeneration of the identity, the user won't be signed out. Issue was reproduced with both the 2.0 and 2.1 rc identity packages, using either OWIN 2.1 and 3.0 rc. If it is the intended design, what's a sensible workaround?

ASP.NET Identity - View Issue #2347: AuthenticationManager.SignOut doesn't work if identity was regenerated in current request

こりゃ何かやらかしたな。

続きを読む

RedisCacheに非同期でアクセスしようとするとハングアップしてしまう

それなりに大規模なミッションでボトルネックになるデータベースを保護するためにキャッシュ機構を作る必要に迫られて、ならばとAzure Redis Cacheを使ってみた。
Redis Session State Providerは普通に動くし動作も快適。

単純なテストでRedisに読み書きするぶんにも特に問題なし。
ところが、

using StackExchange.Redis;
:


private static  ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("127.0.0.1")
private AnyEntity entity;
private static async Task<T> getOrSet<T>(string CacheKey,  bool forceRefresh, Func<int, T> ext) {
    string key = string.Format("{0}_{1}", cachePrefix,  CacheKey);
    IDatabase cache = connection.GetDatabase();
    string cached = await cache.StringGetAsync(key);
    if (cached != null) {
        try {
            return (T)Jil.JSON.Deserialize(cached, typeof(T));
        }catch{}
    }
    var newData = ext(0);
    cached = Jil.JSON.Serialize(newData);
    cache.StringSet(key, cached);
    return newData;
}
private async Task setNames(bool forceRefresh = false) {
    this.Customers = await getOrSet<List<Customer>>("Customers", forceRefresh, (_hid) => {
        return entity.Customers.OrderBy(data => data.Code).ToList();
    });
}

private async Task setTasks(bool forceRefresh = false) {
    this.Tasks = await getOrSet<List<TeamTask>>("Tasks",forceRefresh, (_hid) => {
        return entity.Tasks.OrderBy(data => data.priority).ToList();
    });
}
private async Task setMembers(bool forceRefresh = false) {
    this.Members = await getOrSet<List<Member>>("Members", forceRefresh, (_hid) => {
        return entity.Members.OrderBy(data => data.member_id).ToList();
    });
}

こんな感じの構造にして、

public async Task loadProperties() {
    await Task.WhenAll(new Task[] { this.setNames(), this.setTasks(), this.setMembers() });
}

こんな感じにいざ非同期メソッドを使って読み書きしようとすると応答が無くなってしまう。

へちょん。

続きを読む

列挙型からSelectListItemを生成とか

列挙型プロパティのEditとかでDropdownを使いたいけどSelectListItemを毎回作るコードを書くのは面倒なのでユーティリティ化

  public static class ViewHelper {
        public static IEnumerable<SelectListItem> toList<T>() where T : struct, IConvertible {
            if (!typeof(T).IsEnum) {
                throw new ArgumentException("T must be an enumerated type");
            }
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var v in Enum.GetValues(typeof(T))) {
                items.Add( new SelectListItem(){Value=((int)v).ToString(),Text = Enum.GetName(typeof(T),v)} );
            }
            return items;
        }
}

usage

@Html.DropDownListFor(model => model.modelType, ViewHelper.toList<ModelClass.ModelType>(), new  { @class = "form-control" })

らくちん。

続きを読む