创建和编辑视图共享的部分视图中的主键出现问题.
我有一个局部视图(用户控件),由创建"和编辑"视图共享.当我在编辑"视图中使用它时,我必须包括一个隐藏字段(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.