【问题标题】:editable composite primary key可编辑复合主键
【发布时间】:2012-05-16 06:43:17
【问题描述】:

我有两个字段用作复合主键,它们不是自动生成的。现在在编辑视图中,这两个字段是只读的,不能编辑它们。那么如何进行编辑...?

型号:

[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>
}

【问题讨论】:

  • 显示视图的代码,这样更正更简单。
  • “无法编辑”是什么意思?我使用@Html.EditorForModel 进行了尝试,并且能够更改视图中的关键属性。
  • yaa 无法编辑使用过的@Html.EditorFor。可以编辑复合键以外的其余字段。

标签: c# asp.net-mvc-3 data-annotations composite-primary-key


【解决方案1】:

主键应该是不可变的,我强烈建议使用 ID (int, GUID) 作为主键,然后将它们添加为仅具有唯一约束的代理键。我这样说是因为如果任何数据在任何其他表中引用此数据,您将违反约束,这对您来说将是一场噩梦。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多