2012-04-12 18 views
0

特定のフィールドのデータを編集したいと思います。私が得ている問題は、たとえばフィールド名が文字列であるということです。私はそのフィールドを取得し、jeditableを使ってAjaxを介して、私のコントローラを呼び出す私のモデルの動的フィールド - ASP.NET MVC 3

<div class="editable" id="cnso">@Model.cnso</div> 

$('.editable').editable('/Req/UpdateField', { 
    tooltip: 'Click to edit...', 
    submitdata : { 
     nreqid : function() { 
      return $("#nreqid").val(); 
     } 
    } 
}); 

私のコントローラは、フィールドの名前を得た(私の見解で

私はこのようなパラメータを持っていますcnso)を文字列idに挿入します。問題は私のモデルを更新することです。私のコントローラのいくつかのコード。

public string UpdateField(string id, string value, int nreqid) 
{ 
    /* 
    *  id - id of the field. This value can be used on the 
    *    server side to determine what property of the 
    *    company should be updated. 
    *  value - text that is entered in the input field. 
    * nreqid - this is additional parameter that will 
    *    be added to the request. Value of this parameter 
    *    is a value of the hidden field with an id "ReqId". 
    */ 
    ModelState.Clear(); 

    if (ModelState.IsValid) 
    { 
     //db.Entry(req).State = EntityState.Modified; 
     //db.SaveChanges(); 
     Req req = db.Req.Find(nreqid); 
     req."id" = "2"; // <--- Problem here !!! 
    } 
} 

マイモデルオブジェクト、Reqは、フィールドcnsoを持っている、と私id文字列は「CNSO」を持っているので、どのように私は私の文字列IDからcnsoを選択することができますか?あなたができる反射使用

+0

このReqオブジェクトはどのように見えますか?そのフィールドがあなたの文字列名と一致するreqのフィールドを更新したいのですか? – DMulligan

+0

@AFinkelstein、重要な部分はObject Reqにフィールドcnsoがあることです。私の文字列IDには、割り当てたい文字列 "cnso"があります。 –

答えて

2

:また

Type t = typeof(Req); 
PropertyInfo p = t.GetProperty(id); 
if(p != null) 
    p.SetValue(req, "2", null); 

をあまりにも多くのプロパティが存在しない場合は、あなたが必要とするすべてのプロパティに対して異なる更新アクションを作ることができます。あなたが必要とする状況はわかりませんが、テーブル内の個々のプロパティを一度に更新するのが最適な設計ではないかもしれません。

+0

オハイオ州の男、完璧に動作します。どうもありがとう。 :) –

関連する問題