views:

28

answers:

2

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?

Post Action Code:

    [HttpPost]
    public ActionResult Edit(JobCardViewData viewData)
    {
        try
        {
            jobCardService.Update(viewData.JobCard);

Edit View Excerpt:

<% Html.RenderPartial("JobCardInput", Model); %>

Partial Excerpt:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Poynting.Installation.Web.ViewData.JobCardViewData>" %>
<% using (Html.BeginForm())
A: 

can you have your PK in the url?

ThingController/Create

ThingController/Edit/1234

Anthony Johnston
On the get edit action, it is in the url (value 1), but when I click Save, in the post edit action it is set to 0 in the url, unless I include that hidden field for it.
ProfK
In the create post that does the save, save your data and get the id, the redirect to the edit page with the id
Anthony Johnston
@Anthony, I'm not trying to edit following a create. I'm trying to edit from the list my Index view, and I have the id when I call edit.
ProfK
are you specifying the form post url? could you post the code for your partial and post action?
Anthony Johnston
I've added some code excerpts to the question.
ProfK
your id is on the JobCardViewData object? if so you are not updating it, you are only updating the sub-object JobCardViewData.JobCard
Anthony Johnston
+1  A: 

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.into an identity column.

Leave the hidden field and update the Create action to check if the value is 0 or null (I assume that JobCard is an argument type of the Create action). Another thought would be to re-create a new JobCard object, which excludes setting the id value, and send the new object to the jobCardService create method.

so how can I 'switch' that PK hidden field on or off depending on which action has been invoked?

You could possibly create a HtmlHelper to render the hidden field for you. Check the ViewContext RouteData collection for the the action name output the hidden field accordingly if required. (Pseudo-code..)

public static MvcHtmlString HiddenTest(this HtmlHelper htmlHelper, string name)
{
    string currentAction = htmlHelper.ViewContext.RouteData.Values["action"].ToString();
    if (currentAction = "Edit"){
     return htmlHelper.Hidden(htmlHelper, name);
    }
   return null;
}
Ahmad
@Ahmad, my problem here with your first suggestion is the JobCardId is int, so it's never null, and the value the controller is getting back from the view (when using the hidden field) is 0, which goes with the jobCard to the servive. Your second looks good, and I've actually done alomst that without enclusing in helper. Thanks!
ProfK
Completely agree on the first option - an int will never be null, however in your controller you can check if it is 0 and then re-create a new JobCard object from the posted values.
Ahmad