Disable checkbox only the first time

Im passing in a list of my orders and displaying them:

 @for (int i = 0; i < Model.AllOrders.Count; i++)
     {
    <text>
       <tr>
            <td width="15%">@Model.AllOrders[i].CustomerName</td>   
            <td width="15%">@Model.AllOrders[i].CustomerAddress</td>
            <td width="15%">@Model.AllOrders[i].CustomerPhoneNumber</td>
            <td width="15%">@Model.AllOrders[i].CustomerEmail</td>
            <td width="15%">@Model.ModelBikeMapping[@Model.AllOrders[i].BicycleModel]</td>
            <td width="15%">@Model.SizeMapping[@Model.AllOrders[i].BicycleSize]</td>
            <td width="15%">@Model.ColourMapping[@Model.AllOrders[i].BicycleColour]</td>
            <td width="15%">@Html.CheckBoxFor(x => @Model.AllOrders[i].Shipped)</td>

            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].CustomerName)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].CustomerAddress)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].CustomerPhoneNumber)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].CustomerEmail)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].BicycleModel)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].BicycleSize)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].BicycleColour)</td>
            <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].Shipped)</td>              
       </tr>
    </text>
     }   

</table>
 <p>
     <input type="submit" value="Update" />
 </p>

      

Here the user can check or uncheck the Submitted checkbox and click Update, which passes the new “submitted” value back to the controller, which updates the db table. This all works great. However, if the checkbox is TRUE, I want to disable it since you can only submit an item once.

iv tried:

@if (@Model.AllOrders[i].Shipped)
            {
                <td width="15%">@Html.CheckBoxFor(x => @Model.AllOrders[i].Shipped, new { disabled = "disabled" })</td>
                <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].Shipped)</td>  
            }
            else
            {
                <td width="15%">@Html.CheckBoxFor(x => @Model.AllOrders[i].Shipped)</td>
                <td width="15%">@Html.HiddenFor(item => Model.AllOrders[i].Shipped)</td>  
            }

      

The problem is that the user can update a new record with unshipped [F] to load [T] and db updates correctly, but all existing TRUE values ​​are "sent" as FALSE is "carried" to the controller, Is this something something related to my logic or passing hidden? Please inform

+3


source to share


1 answer


I believe your problem is related to how the coupler simulates your model.

Based on what I can see, I would expect your model to look something like

public class ModelType
{
public List<Order> AllOrders {get; set;}
}

      

When you try to skip this in your form and send it back to the server, it gets a little scared. The quickest and easiest way to fix this is to add a property to your model and then place that as a hidden field in your form.



public class ModelType
{
public List<Order> AllOrders {get; set;}
public int Id {get; set;}
}

      

Then add the Id to your form as a hidden value.

I believe the correct way to fix this is to use Html.EditorFor where you have to remove the for loop and replace it with your editor. I ran into some oddities with the editor, mainly that he wants to be named the same as your T from the list and in a shared folder. But I would recommend looking into it, however adding that Id prop (or any other really) should be a quick fix for you.

0


source







All Articles