编辑时保存在asp.net mvc中时缺少fk值 [英] Edit missing the fk value while saving in asp.net mvc

查看:66
本文介绍了编辑时保存在asp.net mvc中时缺少fk值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一张名为contract的表,有以下字段



Id(uniqueidentifier ),contractno(int),personname(varchar(50)),NATIONALId(fk,uniqueidentifier),

OccupationId(FK,uniqueidentifier),SalaryId(fk,Uniqueidentifier)。



工资已在salarydetails表中定义,其中包含以下基于职业和国籍的字段



Id(pk,唯一标识符) ,OccupationId(fkuniqueidentifier,ud),NATIONALId(fk,uniqueidentifier),薪水(int)



我使用asp.net mvc创建合同,这是有效的罚款



但问题在于编辑,它会加载数据,但是虽然缺少保存工资ID,所以model.isvalid变为false



视图中的代码如下:

 <   div     class   =  editor-field >  
@ Html.TextBoxFor(model => model.SalaryDetail.Salary)
@ Html.ValidationMessageFor(model => model.SalaryId)
< / div >



而不是salarydetail.salary,如果我把SalaryId然后编辑表单从表中加载id而不是工资值,如果它工作但我应该显示工资而不是id



非常感谢您的帮助。



已添加代码块[/编辑]

解决方案

这也取决于你的模型。但是我想你不希望用户输入GUID',你就不能为引用的字段创建编辑器 - 它不在你编辑的模型实例的范围内(你可以显示)它)。在我看来,你需要在那里使用DropDownList。有几种选择如何填充它,在这里查看其中一个: http://odetocode.com/blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx [<一个href =http://odetocode.com/blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx\"target =_ blanktitle =新窗口 > ^ ]

Hi,

I have a table called contract, has following fields

Id(uniqueidentifier),contractno(int), personname(varchar(50)),NationalityId(fk,uniqueidentifier),
OccupationId(FK,uniqueidentifier), SalaryId(fk,Uniqueidentifier).

Salary has been defined in the salarydetails table with the following fields based on occupation and nationality

Id(pk,unique identifier),OccupationId(fkuniqueidentifier,ud),NationalityId(fk,uniqueidentifier),salary(int)

I am using asp.net mvc to create a contract, which is working fine

but the problem is in edit, which loads the data, but while saving salary id is missing, so model.isvalid is becomes false

The code in the view is given below

<div class="editor-field">
         @Html.TextBoxFor(model => model.SalaryDetail.Salary)
         @Html.ValidationMessageFor(model => model.SalaryId)
</div>


Instead of salarydetail.salary, if I put SalaryId then the edit form loads the id instead of salary value from the table, in the case its working but i supposed to display the salary not the id

Your help will be much appreciated.

[Edit]Code block added[/Edit]

解决方案

It depends on your model too. But I suppose you don''t want the user to enter GUID''s, and you won''t be able to make editor for a referenced fields - it is not in the scope of your edited model instance (you could however display it). It looks to me that you need to use a DropDownList there. There are several alternatives how to populate it, look here for one of them: http://odetocode.com/blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx[^]


这篇关于编辑时保存在asp.net mvc中时缺少fk值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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