0

I have a class from EF.

public partial class Customer
{
    public Customer()
    {
        this.Customer_CustomerSpecialConcern = new HashSet<Customer_CustomerSpecialConcern>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public virtual ICollection<Customer_CustomerSpecialConcern> Customer_CustomerSpecialConcern { get; set; }
}

When I pass the model from controller to view everything works fine (can access Customer_CustomerSpecialConcern values). The problem is when I post back model to a controller to save the changes the property Customer_CustomerSpecialConcern is null.

Here is how I use it in view.

@foreach (var ccsc in Model.Customer_CustomerSpecialConcern)
{
    <div class="form-group fields-container col-md-3">
        <label class="field-label control-label col-md-10" for="">@ccsc.CustomerSpecialConcern.Title</label>
        <div class="col-md-1 field-input">
            @Html.EditorFor(model => ccsc.Value)
            @Html.HiddenFor(model => ccsc.Value)
        </div>
    </div>
}

Please, I need help to get the values of this collection property to controller. Thank you.

Update - Customer_CustomerSpecialConcern class details

public partial class Customer_CustomerSpecialConcern 
{ 
    public int Id { get; set; } 
    public int Customer_Id { get; set; } 
    public int CustomerSpecialConcern_Id { get; set; } 
    public bool Value { get; set; } 
    public virtual Customer Customer { get; set; } 
    public virtual CustomerSpecialConcern CustomerSpecialConcern { get; set; } 
}
vendettamit
  • 14,315
  • 2
  • 32
  • 54
Abraham
  • 5
  • 8

1 Answers1

2

Please try this,

@for (int i = 0; i < Model.Customer_CustomerSpecialConcern.Count(); i++)
{
    <div class="form-group fields-container col-md-3">
        <label class="field-label control-label col-md-10" for="">@Model.CustomerSpecialConcern[i].Title</label>
        <div class="col-md-1 field-input">
            @Html.EditorFor(model => model.Customer_CustomerSpecialConcern[i].Value)
            @Html.HiddenFor(model => model.Customer_CustomerSpecialConcern[i].Value)
        </div>
    </div>
}

Check this article.

I tried your example, and this is how it looks

public ActionResult Index()
        {
            var customer = new Customer
            {
                Name = "Name",
                Surname = "Surname",
                Email = "email@email.com",
                Mobile = "mobile...",
                Customer_CustomerSpecialConcern = new List<Customer_CustomerSpecialConcern>
                {
                    new Customer_CustomerSpecialConcern
                    {
                        Value = true
                    },
                    new Customer_CustomerSpecialConcern
                    {
                        Value = true
                    }
                }
            };

            return View(customer);
        }

View:

@model WebApplication1.Models.Customer

@{
    ViewBag.Title = "Customer";
    var customer_CustomerSpecialConcern = Model.Customer_CustomerSpecialConcern.ToList();
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    for (int i = 0; i < Model.Customer_CustomerSpecialConcern.Count(); i++)
    {
        <div class="form-group fields-container col-md-3">
            <label class="field-label control-label col-md-10" for=""></label>
            <div class="col-md-1 field-input">
                @Html.CheckBoxFor(model => customer_CustomerSpecialConcern[i].Value)
            </div>
        </div>
    }
    <input type="submit" value="Save"/>
}
Gedao
  • 1,025
  • 9
  • 13
  • i tried but nothing... – Abraham Feb 16 '16 at 18:03
  • Why are you using EditorFor and HiddenFor helpers for same field? – Gedao Feb 16 '16 at 18:04
  • I was trying to get the value so I tried everything – Abraham Feb 16 '16 at 18:06
  • Can you provide code for Customer_CustomerSpecialConcern model? – Gedao Feb 16 '16 at 18:10
  • `public partial class Customer_CustomerSpecialConcern { public int Id { get; set; } public int Customer_Id { get; set; } public int CustomerSpecialConcern_Id { get; set; } public bool Value { get; set; } public virtual Customer Customer { get; set; } public virtual CustomerSpecialConcern CustomerSpecialConcern { get; set; } }` – Abraham Feb 16 '16 at 18:11
  • Please, check my updated answer. – Gedao Feb 16 '16 at 18:31
  • 2
    @EdinMahmutovic Kindly avoid screenshot of code snippets whenever possible, instead add code snippet itself in the post. This would add more value to your answer. – vendettamit Feb 16 '16 at 18:39
  • Thank you very much!! You solve my problem!!! – Abraham Feb 16 '16 at 18:46