0

in a MVC C# View I show the records of specifics employees, for that I use MVCScaffolding and the model below

 public class Developer
    {
        public int code { get; set; }
        public string areaDev { get; set; }
        public string nameDev { get; set; }
        public string expDev { get; set; }
        public string langDev { get; set; }
    }

the view uses razor and for every record there is a checkbox input

@model IEnumerable<WebApplication1.Models.Developer>

@using(Html.BeginForm("ShowRecords","Home"))
{

<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.code)</th>
        <th>@Html.DisplayNameFor(model => model.areaDev)</th>
        <th>@Html.DisplayNameFor(model => model.nameDev)</th>
        <th>@Html.DisplayNameFor(model => model.expDev)</th>
        <th>@Html.DisplayNameFor(model => model.langDev)</th>
        <th>select</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.code)</td>
        <td>@Html.DisplayFor(modelItem => item.areaDev)</td>
        <td>@Html.DisplayFor(modelItem => item.nameDev)</td>
        <td>@Html.DisplayFor(modelItem => item.expDev)</td>
        <td>@Html.DisplayFor(modelItem => item.langDev)</td>
        <td><input type="checkbox" name="code" value="@item.code" /></td>
    </tr>
}
</table>
<input type="submit" value="SEND" />
}

and what I want is to retrieve the information of code(integer code) when the user click the checkbox of one/many specific records displayed in the view,

enter image description here

for that in my controller I receive a int[] as shown below

  public ActionResult ShowRecords(int[] datos)
    {
        {
            foreach(var item in datos)  
            {  ... some code goes here ... }     
            return View(); 
        }

But I don't receive anything from the view, always receive NULL

enter image description here

could you please help me and tell how to retrieve the code info of the due checked row in my controller?

Edit: just added the isChecked property

public class Developer
    {
        public int code { get; set; }
        public string areaDev { get; set; }
        public string nameDev { get; set; }
        public string expDev { get; set; }
        public string langDev { get; set; }
        public bool isChecked { get; set; }
    }

the controller that sends info to the view has the new property sets to false(in order to not present the checkbox checked)

while (dr.Read())
                {
                    Models.Developer data = new Models.Developer();
                    data.code = Convert.ToInt32(dr["code"]);
                    data.areaDev = dr["areaDev"].ToString();
                    data.nameDev = dr["nameDev"].ToString();
                    data.expDev = dr["expDev"].ToString();
                    data.langDev = dr["langDev"].ToString();
                    data.isChecked = false;
                    Records.Add(data);
                }

in my View I added this

  @Html.CheckBoxFor(modelItem => item.isChecked)

and in the controller I expects to receive a list of developer model

public ActionResult ShowRecords(List<WebApplication1.Models.Developer> datos)

but stills receive NULL

3 Answers3

2

Your generating checkboxes with name="code" therefore you POST method signature needs to be

public ActionResult ShowRecords(int[] code)

The code parameter will contain an array of the values of the checked checkboxes.

Based on your edit using a view model, your view will need to use a for loop or custom EditorTemplate so that the name attributes are generated correctly for binding to a collection (refer Post an HTML Table to ADO.NET DataTable for more detail)

@for (int i = 0; i < Model.Count; i++)
{
    <td>
        @Html.CheckBoxFor(m => m[i].code) // include hidden input so its submitted
        @Html.DisplayFor(m=> m[i].code)
    </td>
    ....
    <td>@Html.CheckBoxFor(m => m[i].isChecked)</td>
}

and the POST method signature would be

public ActionResult ShowRecords(List<Developer> model)
Community
  • 1
  • 1
  • See updated answer based on using a view model for `Developer` –  May 01 '17 at 00:40
  • thanks, the two answers did work fine, just one more quickly question, Why should I do if I want to display a message if the user click the button SEND but there is not any checkbox checked? –  May 01 '17 at 01:31
  • You can write a javascript function that checks at least one is checked and if not, cancel the submit and display an error message (I'll find a link for you a bit later). You should also handle it server side by adding a `ModelStateError` and return the view if the array is empty. –  May 01 '17 at 02:03
  • Refer [this answer](http://stackoverflow.com/questions/43546067/how-to-validate-a-list-of-checkbox-through-custom-jquery-in-asp-net-mvc/43553939#43553939) for an example - but you really should be using the view model approach - i.e. create a class `DeveloperVM` with a `bool IsSelected` property so that you can return the view if its invalid. –  May 01 '17 at 02:05
0

Change the parameter of your action to IEnumerable<WebApplication1.Models.Developer> and then inspect the isChecked property of each of these (or just use LINQ to filter it down). If you don't like that approach, you could use Javascript to to return those ints. I will point out that returning the model while using more bandwidth (because it's more data going back to the server) but it conforms with the MVC pattern more than returning the ints.

chris-crush-code
  • 1,114
  • 2
  • 8
  • 17
0

You can create a viewModel with a Developper object and an additional isChecked property. Then modify your view to use:

IEnumerable <DevelopperViewModel>

as Model for your view and bind the value of isChecked.

Etienne
  • 1,058
  • 11
  • 22
  • Thanks, I just did edit my question with your suggest, could you please check and help me if you can. –  May 01 '17 at 00:33