Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900