如何在索引页面和控制器中映射复合键 [英] How to map composite key in Index page and controller

查看:78
本文介绍了如何在索引页面和控制器中映射复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的型号:

<前lang =cs> [键,列(订单= 0 )]
[ForeignKey( comp)]
public int comp_num { get ; set ; }
public virtual Comp Comp { get < /跨度>; set ; }
[Key,Column(Order = 1 )]
[ForeignKey( Master)]
public string part { get ; set ; }
public virtual Master Master { get < /跨度>; set ; }
public string comp_part_num { get ; set ; }





我的索引页:



- -------------------------------------------------- -------

----------------------------------- ------------------------

@foreach(模特中的var项目){

@ Html.DisplayFor(modelItem => item.comp)

@ Html.DisplayFor(modelItem => item.part)

@ Html.DisplayFor(modelItem => item.comp_part_num)

@ Html.ActionLink(编辑,编辑,新{id = item.com})|

@ Html.ActionLink(详细信息,详细信息,新{id = item.comp})|

@ Html.ActionLink(删除,删除 ,new {id = item.comp})

}



当我在id中只使用一个键时它给出错误'传递的主键值的数量必须与实体上定义的主键值的数量'



并且还给出如何在控制器中使用

  public  ActionResult Edit( int  id =  0 
{
RecordDataView record = db.RecordDataView.Find(id);
if (record == null
{
return HttpNotFound();
}
return 查看(记录);
}





有人请帮忙。

解决方案

I解决了它。

只需在actionlink中添加另一个参数



@ Html.ActionLink(编辑,编辑,新{ id = item.com,val = item.part})





和控制器



公共Actionlink编辑(int id,string val)

{

RecordDataView record = db.RecordDataView.Find(id,val);

..............

..............

}

My Model:

[Key,Column(Order = 0)]
[ForeignKey("comp")]
public int comp_num { get; set; }
public virtual Comp Comp { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Master")]
public string part { get; set; }
public virtual Master Master { get; set; }
public string comp_part_num { get; set; }



My Index page:

-----------------------------------------------------------
-----------------------------------------------------------
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.comp)
@Html.DisplayFor(modelItem => item.part)
@Html.DisplayFor(modelItem => item.comp_part_num)
@Html.ActionLink("Edit", "Edit", new { id=item.com }) |
@Html.ActionLink("Details", "Details", new { id = item.comp }) |
@Html.ActionLink("Delete", "Delete", new { id = item.comp })
}

when I use only one key in id it gives the error 'The number of primary key value passed must match number of primary key values defined on the entity'

and also give how to use in controller

public ActionResult Edit(int id = 0)
    {
         RecordDataView record = db.RecordDataView.Find(id);
            if (record == null)
            {
                return HttpNotFound();
            }
            return View(record);
        }



Someone please help.

解决方案

I got resolved it.
just add another parameter to actionlink as

@Html.ActionLink("Edit", "Edit", new { id=item.com, val=item.part })


and in controller

Public Actionlink Edit(int id, string val)
{
RecordDataView record = db.RecordDataView.Find(id,val);
..............
..............
}


这篇关于如何在索引页面和控制器中映射复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆