3

I have a problem in my application I am using MVC 3 with razor and i want to get the value of checked box in the form

@using (Html.BeginForm("ConfirmItemAvilabilty", "Order", FormMethod.Post, new { id = "frmConfirmAvilabilty", name = "frmConfirmAvilability" }))
{
    foreach (OrderItem orderItem in orderAction.Order.OrderItems)
    {
    <div class="product">
        <ul>
            <li>
                <div class="productImg">
                    <img src="@orderItem.Product.Image" width="100px" height="100px"/>
                </div>
                <div class="centered">
                    <a href="@orderItem.Product.Link" target="_blank">Link</a> <span>@orderItem.Product.TitleAr</span>
                    <span>@orderItem.Product.Price</span>
                </div>
                @if (currentUser.UserTypeEnum == UserTypeEnum.Reseller)
                {
                    <div>
                        @Html.CheckBox("ChkConfirm", orderItem.IsAvailable, new { id="chkConfirm" ,@class="chkConfirm"})
                        @Html.Hidden("OrderItemId", orderItem.Id, new { id="hdnConfirm"})
                    </div>
                }
            </li>
        </ul>
    </div>
    }
    if (currentUser.UserTypeEnum == UserTypeEnum.Reseller)
    { 
        <button>Confirm</button>
    }
}

Simply i want to get the value of all checked checkboxs i tryed to create a model holding the value of my checkbox and the value of the hidden text below it

public class ItemOrderModel
{
    public string ChkConfirm { get; set; }
    public string OrderItemId { get; set; }
}

and in my controller i do the following but nothing happened

   public ActionResult ConfirmItemAvilabilty(List<ItemOrderModel> OrderItems)
    {

        return View();
    }

but orderItems always returns null, Can anyone help me in that?

----------------- Edit ------------------ Thank you Sam and Jesse

I Found a solution for my problem but I am facing another problem now first of all i solved my problem by changing in model view to be like that

public class ItemOrderModel
{
    public List<bool> ChkConfirms { get; set; }
    public List<string> OrderItemId { get; set; }
}

and change the checkbox name to be

  @Html.CheckBox("ChkConfirms", orderItem.IsAvailable, new { id = "chkConfirm", @class = "chkConfirm" })

the problem now is when i submit i found two values false that is the actual representation of my checkboxs and two ids that's also the actual representation of my hidden fields "Correct scenario" when i check one of check boxs in the same form i found 3 result for the check box and 2 results for the hidden field Can any one help in that or have a solution

Mohamed Salah
  • 959
  • 10
  • 40
  • May be worth a read: http://byatool.com/mvc/asp-net-mvc-multiple-checkboxes-and-strongly-typed-views/ – Jesse Nov 02 '11 at 22:34

1 Answers1

6

You need to look into Editor Templates:

How to create custom editor/display templates in ASP.NET MVC 3?

These allow you to do the exact thing you are talking about.

Community
  • 1
  • 1
Sam
  • 15,336
  • 25
  • 85
  • 148
  • How that is solving my question that is talking about loading templates using models that wasn't my question – Mohamed Salah Nov 02 '11 at 22:29
  • @MohamedSalah - Read about Editor Templates, they solve the exact issue you are having. You are using editors (hence the @Html.CheckboxFor). – Sam Nov 02 '11 at 22:37
  • 1
    @MohamedSalah - You can define a custom editor template for that class, and you can define the exact markup that you are wanting. – Jamie Nov 03 '11 at 00:57