Marta
Marta

Reputation: 2947

How to pass value from checkbox to controller?

I have got a view that contain list nad filter options. I need to add option to update selected items on list at once. View with list and filter options looks in short like that:

    @model GWeb.Models.FilterModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
   <fieldset>
        <legend>Search criteria</legend>
            @Html.LabelFor(model => model.ProjectId, "Project")       
            @Html.DropDownList("ProjectId",
            new SelectList(ViewBag.projectListDesciption as System.Collections.IEnumerable, "Id", "Desciption"), "all")

            @Html.LabelFor(model => model.StartWork, "From Date")       
            @(Html.Telerik().DatePicker()
            .Name("StartWork")
            .Value(Model.StartWork))  

        <input type="submit" value="Filter" name="submitButton"/>
    </fieldset>  
    <fieldset>
        <legend>Status change</legend>        
            Change status on selected:        
            @Html.DropDownList("Status",
             new SelectList(ViewBag.statusList as System.Collections.IEnumerable, "Id", "Description"))

        <input type="submit" value="Update" name="submitButton"/>
    </fieldset>
}

@{Html.RenderPartial("WorkList", Model.workList);}

Where list is rendered in partial view that in short looks like that:

@model IEnumerable<GWeb.Models.WorkModel>   
    @foreach (var item in Model)
    {
        <tr>
            <td>
            @Html.CheckBox("chb" + item.Id, item.Selected)
            </td>
            ...
        </tr>
    }

Here are models that are used:

        public class FilterModel
    { 
        public int ProjectId { get; set; }         
        public DateTime? StartWork { get; set; }  
        public int? Status { get; set; }

        public List<WorkModel> workList { get; set; }
    }

    public class WorkModel
    {
        public int Id { get; set; }
        ...         
        public bool Selected { get; set; }
    }

In controller I am checking what button was clicked. If it was 'Update' I want to perform modifications. But the workFilter.workList is always null.

public ActionResult WorkManager(FilterModel workFilter, string submitButton)
    {            
        if (submitButton == "Update")
        {
            if (workFilter.workList != null)
            {
                //...
            }             
        }
        else
        {
           //filter
        }
        //...           
        return View(workFilter);

How can I check in controller wchich checkbox was selected to update?

Any help much appreciated!

Upvotes: 0

Views: 1698

Answers (2)

Cam
Cam

Reputation: 378

To avoid the workList is NULL problem, you can add code to your FilterModel default constructor (parameterless constructor) to create and initialize workList with dummy items.

Please let me know if this solves the problem.

Update:

Excuse me for not clarifying the effect of adding code to the constructor. No, it will not create a new model. What it does is initialize workList so that the Controller can access the indexed elements within it.

Sample code:

public FilterModel(){
  workList = new List<WorkModel>();
  for(int i = 0; i < MAX_WORKMODEL_COUNT; i++){
    workList.Add(new WorkModel());
  }
}

Upvotes: 0

MiBu
MiBu

Reputation: 869

You should look How to produce non-sequential prefix collection indices with MVC HTML Editor templates? because for anything little more complicated you'll need to do something like that.

Quick solution to your problem would be:

//instead of: @{Html.RenderPartial("WorkList", Model.workList);} write code below inside using statement(below </fieldset>)


    @for (int i = 0; i < Model.worklist.Count; i++)
    {
        <tr>
            <td>
            @Html.HiddenFor(x => x.workList[i].Id)
            @Html.CheckBoxFor(x => x.workList[i].Selected)
            </td>
            ...
        </tr>
    }

warning: above code is not tested, but I advise you to look at the answer on top.

Upvotes: 1

Related Questions