tags:

views:

17

answers:

2

Hi I have a really strange ‘bug’. I use this in my view:

<% foreach (var QualitativeGlobalFeatureValue in Model.PossibleValues)
                            { %>
                                <% using (Html.BeginForm("DeleteQualitativeGlobalFeatureValue", "Features", FormMethod.Post, new { @class = "deleteForm" }))
                                   { %>
                                        <%= QualitativeGlobalFeatureValue.Value %>
                                        <%= Html.ActionLink("Edit", "QualitativeGlobalFeatureValueForm", new { FeatureId = Model.Id, Id = QualitativeGlobalFeatureValue.Id })%>
                                        <%= Html.Hidden("QualitativeGlobalFeatureValueId", QualitativeGlobalFeatureValue.Id)%>
                                        <%= QualitativeGlobalFeatureValue.Id %>
                                        <%= Html.Hidden("FeatureId", Model.Id)%>
                                        <input type="submit" value="Delete" class="link_button" /> 
                                <% } %>
                        <% } %>

This produces a bunch of forms which post to an action which then redirect to an action which in turn produces this view.

Here is some HTML:

<form action="/Features/DeleteQualitativeGlobalFeatureValue" class="deleteForm" method="post">b
                                        <a href="/Features/QualitativeGlobalFeatureValueForm?FeatureId=2103&amp;Id=3004">Edit</a>
                                        <input id="QualitativeGlobalFeatureValueId" name="QualitativeGlobalFeatureValueId" value="3004" type="hidden">
                                        3004
                                        <input id="FeatureId" name="FeatureId" value="2103" type="hidden">
                                        <input value="Delete" class="link_button" type="submit"> 
                                </form><form action="/Features/DeleteQualitativeGlobalFeatureValue" class="deleteForm" method="post">aa
                                        <a href="/Features/QualitativeGlobalFeatureValueForm?FeatureId=2103&amp;Id=9010">Edit</a>
                                        <input id="QualitativeGlobalFeatureValueId" name="QualitativeGlobalFeatureValueId" value="9010" type="hidden">
                                        9010
                                        <input id="FeatureId" name="FeatureId" value="2103" type="hidden">
                                        <input value="Delete" class="link_button" type="submit"> 
                                </form>

Now if I delete the value with the Id 9010 the resulting HTML is as follows:

<form action="/Features/DeleteQualitativeGlobalFeatureValue" class="deleteForm" method="post">b
                                        <a href="/Features/QualitativeGlobalFeatureValueForm?FeatureId=2103&amp;Id=3004">Edit</a>
                                        <input id="QualitativeGlobalFeatureValueId" name="QualitativeGlobalFeatureValueId" value="9010" type="hidden">
                                        3004
                                        <input id="FeatureId" name="FeatureId" value="2103" type="hidden">
                                        <input value="Delete" class="link_button" type="submit"> 
                                </form>

For some unexplainable reason it contains value="9010" rather than value="3004" although it uses the code QualitativeGlobalFeatureValue.Id

It just does not make sense. Is this some browser/caching issue? – I am using Firefox. Thanks!

Best wishes,

Christian

PS:

Actions:

[MembersOnlyAttribute]
        [AcceptVerbs(HttpVerbs.Get)]
        public ViewResult GlobalQualitativeFeature(string Id)
        {
            QualitativeGlobalFeature QualitativeGlobalFeature = null;

            if (TempData["ViewData"] != null)
            {
                ViewData = TempData["ViewData"] as ViewDataDictionary;
            }

            try
            {
                QualitativeGlobalFeature = FeatureService.GetQualitativeGlobalFeature(Id);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Exception", e.Message);
            }

            return View("GlobalQualitativeFeature", QualitativeGlobalFeature);
        }


[MembersOnlyAttribute]
        [AcceptVerbs(HttpVerbs.Post)]
        public RedirectToRouteResult DeleteQualitativeGlobalFeatureValue(string QualitativeGlobalFeatureValueId, string FeatureId)
        {
            try
            {
                FeatureService.GetQualitativeGlobalFeatureValueRepository().DbContext.BeginTransaction();
                FeatureService.DeleteQualitativeGlobalFeatureValue(QualitativeGlobalFeatureValueId);
                FeatureService.GetQualitativeGlobalFeatureValueRepository().DbContext.CommitTransaction();
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Exception", e.Message);
                FeatureService.GetQualitativeGlobalFeatureValueRepository().DbContext.RollbackTransaction();
            }

            TempData["ViewData"] = ViewData;

            return RedirectToAction("GlobalQualitativeFeature", new { Id = FeatureId });
        }
A: 

Hey,

You can put breakpoints in the markup and debug as it renders through, though it doesn't allow putting breakpoints on client markup or <% lines, so you need to find a line continuation.

Are you sure that it isn't a sort reordering or something like that, maybe the results aren't sorted, and that result is later on?

HTH.

Brian
Thanks for the reply. For the edit bit it uses: Id = QualitativeGlobalFeatureValue.Id which produces: Id=3004. The hidden value QualitativeGlobalFeatureValueId also uses QualitativeGlobalFeatureValue.Id but still has the definatley deleted value - very strange!
csetzkorn
+1  A: 

I suspect the following. You click on the delete button for the 9010. The form is posted and the POST request contains QualitativeGlobalFeatureValueId=9010. In the controller action the same view is rendered. Here's the gotcha. When you write this:

<%= Html.Hidden(
    "QualitativeGlobalFeatureValueId", 
    QualitativeGlobalFeatureValue.Id)
%>

The HTML helper (and not only this one) will first look if there's a request parameter with the same name as the name of the field (QualitativeGlobalFeatureValueId) and will use this value instead of the one you specified as the second argument (that's the way it is, don't ask my why, it's by design). So to fix this the only way is to manually render the hidden field:

<input 
    id="QualitativeGlobalFeatureValueId" 
    name="QualitativeGlobalFeatureValueId" 
    value="<%= QualitativeGlobalFeatureValue.Id %>" 
    type="hidden" 
/>
Darin Dimitrov
This helped - thanks. Strange that this is necessary
csetzkorn