创建和编辑视图共享的部分视图中的主键出现问题. [英] Problem with Primary Key in a Partial View shared by Create and Edit views.

查看:112
本文介绍了创建和编辑视图共享的部分视图中的主键出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个局部视图(用户控件),由创建"和编辑"视图共享.当我在编辑"视图中使用它时,我必须包括一个隐藏字段(Html.HiddenFor),以防止数据服务中出现行已更改或未找到"错误,但是当我在创建"视图中使用它时,我必须删除PK隐藏字段,以防止尝试插入标识列时出错.
在此应用程序中不使用标识列是不可行的,因此如何根据调用了哪个动作来打开"或关闭PK隐藏字段?

I have a partial view (user control) that is shared by my Create and Edit views. When I use it in the Edit view, I have to to include an hidden field (Html.HiddenFor) to prevent a ''Row changed or not found'' error in my data service, but when I use it in the Create view, I have to remove the PK hidden field, to prevent an error over trying to insert into an identity column.
It is not feasible to not use identity columns in this application, so how can I ''switch'' that PK hidden field on or off depending on which action has been invoked?

推荐答案

作为建议,您可以使用ViewData传递操作类型,并避免创建控件.像这样:

在编辑视图页面中
As a suggestion you can use ViewData to pass the type of action and avoid creating the control. Like this:

In the edit view page
Html.RenderPartial("YourControl", Model, new { actionType = "edit" });



在创建视图页面中



In the create view page

Html.RenderPartial("YourControl", Model, new { actionType = "create" });>



在控制视图页面中



In the control view page

if (ViewData["actionType"] == "edit") { 
    Html.HiddenFor(m => m.Id)
}



这样,仅当从编辑"视图页面调用控件时,才会显示隐藏字段.



Like this the hidden field will only be rendered when the control is called from the Edit view page.


这篇关于创建和编辑视图共享的部分视图中的主键出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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