2012-05-16 78 views
2

我有兩個字段被用作複合主鍵,它們不是自動生成的。現在在編輯視圖中,這兩個字段是隻讀的,無法編輯它們。那麼編輯怎麼做......?可編輯複合主鍵

型號:

[Key][Column (Order=0)] 
[Required] 
public string LOV_COLUMN { get; set; } 
[Key][Column(Order = 1)] 
[Required] 
public string LOV_CODE { get; set; } 
[Required] 
public string LOV_DESC{ get; set; } 
public string COLUMN_TYPE { get; set; } 
public string LOV_STATUS { get; set; } 

控制器:

public ActionResult Edit(string LOV_COLUMN= "", string LOV_CODE="") 
{ 
    return View(dv.LOV.Find(LOV_COLUMN, LOV_CODE) ?? new ADM_LOV_Master()); 
} 

[HttpPost] 
public ActionResult Edit(ADM_LOV_Master entity, FormCollection form) 
{ 
    try 
    { 
     var model = dv.LOV.Find(entity.LOV_COLUMN, entity.LOV_CODE); 
     TryUpdateModel<ADM_LOV_Master>(model, form.ToValueProvider()); 
     dv.Entry<ADM_LOV_Master>(model).State = System.Data.EntityState.Modified; 
     dv.SaveChanges(); 
     return RedirectToAction("Index");     
    } 
    catch 
    {    
     return View(); 
    } 
} 

Edit View Code as: 

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>LOV_Master</legend>  

    <table> 
    <tr> 
    <td> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.LOV_COLUMN) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.LOV_COLUMN) 
     @Html.ValidationMessageFor(model => model.LOV_COLUMN) 
    </div> 
    </td> 
    <td> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.LOV_CODE) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.LOV_CODE) 
     @Html.ValidationMessageFor(model => model.LOV_CODE) 
    </div> 
    </td> 
    <td> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.LOV_DESC) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.LOV_DESC) 
     @Html.ValidationMessageFor(model => model.LOV_DESC) 
    </div> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.COLUMN_TYPE) 
    </div> 
    <div class="editor-field"> 
     @*@Html.EditorFor(model => model.COLUMN_TYPE)*@ 
     @Html.RadioButtonFor(model => model.COLUMN_TYPE, "C", new { id = "COLUMN_TYPE_false"}) 
     <label for="COLUMN_TYPE_true">Character</label>    
     @Html.RadioButtonFor(model => model.COLUMN_TYPE, "N", new { id = "COLUMN_TYPE_true"}) 
     <label for="COLUMN_TYPE_true">Number</label>       
     @Html.ValidationMessageFor(model => model.COLUMN_TYPE)    
    </div>   
    </td> 
    <td> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.LOV_STATUS) 
    </div> 
    <div class="editor-field"> 
     @*@Html.EditorFor(model => model.LOV_STATUS)*@ 
     @Html.RadioButtonFor(model => model.LOV_STATUS, "Y", new { id = "LOV_STATUS_true"}) 
     <label for="LOV_STATUS_true">Yes</label>     
     @Html.RadioButtonFor(model => model.LOV_STATUS, "N", new { id = "LOV_STATUS_false" }) 
     <label for="LOV_STATUS_true">No</label>  
     @Html.ValidationMessageFor(model => model.LOV_STATUS) 
    </div> 
    </td> 

    </tr> 

</table> 

</fieldset> 
} 
+0

顯示視圖的代碼,更簡單的方法發佈更正。 – linkerro

+0

你是什麼意思「無法編輯」?我使用@ Html.EditorForModel嘗試了這一點,並且可以對視圖中的關鍵屬性進行更改。 – McGarnagle

+0

發佈編輯視圖的代碼。 – user13

回答

3

主鍵應該是不可變的,我會強烈鼓勵使用ID(INT,GUID)作爲主鍵即然後將它們添加爲僅具有唯一約束的代理鍵。我這樣說是因爲如果有任何數據在其他表中引用了這些數據,那麼您將受到違反約束條件的限制,這對您而言將是一場噩夢。