0

Using - ASP.NET MVC 4

UI is like -

Select      Product Code      Product Name

Checkbox    Product 1         Apple
Checkbox    Product 2         Banana
Checkbox    Product 3         Grapes

Submit Button

On submit button click i need to validate whether checkbox selected or not. If not, shows message - Please select one product.

If selected, then in the controller, pass the selected Product Code & Name & no. of checkboxes checked to another page

How can we achieve this in MVC 4?

1
  • Also it can be multiple values that can be passed. Commented Dec 9, 2014 at 18:41

1 Answer 1

1

Firstly, @Html.CheckboxFor(m => m.Checkbox, "Product 1") is assigning "Product 1" as a html attribute and will render <input length="9" type="checkbox" .../> (the length attribute has been added with a value equal the number of characters in the text). Its also creating invalid html because of duplicate id attributes. And the [Required] attribute is pointless (a bool is either true or false and will always be required). Currently you have no relationship between the checkbox and product.

You should create a view model to represent what you want to display

public class ProductVM
{
  public bool IsSelected { get; set; }
  public string Code { get; set; }
  public string Name { get; set; }
}

Controller

public ActionResult Edit()
{
  List<ProductVM> model = new List<ProductVM>()
  {
    new ProductVM() { Code = "Product 1", Name = "Apple" },
    new ProductVM() { Code = "Product 2", Name = "Banana" },
    new ProductVM() { Code = "Product 3", Name = "Grapes"}
  };
  return View(model);
}

[HttpPost]
public ActionResult Edit(List<ProductVM> model)
{
  foreach(ProductVM item in model)
  {
    if (item.IsSelected)
    {
      string code = item.Code;
      string name= item.Name;
    }
  }
}

View

@model List<ProductVM>
@using(Html.BeginForm())
{
  for(int i = 0; i < Model.Count; i++)
  {
    @Html.HiddenFor(m => m[i].Code)
    @Html.HiddenFor(m => m[i].Name)
    @Html.CheckBoxFor(m => m[i].IsSelected)
    @Html.LabelFor((m => m[i].IsSelected, Model[i].Name)
  }
  <input type="submit" />
}

If you want client side validation, you are going to have to write your own validation attribute that implements IClientValitable and write the associated jquery validation methods (but that's the subject of a separate question)

Sign up to request clarification or add additional context in comments.

9 Comments

but in the Edit action method, how can we get the checked checkbox value.
Sorry, the POST method should be Edit(List<ProductVM> model), then all you need is a loop foreach(ProductVM item in model) { if(item.IsSelected) { //do something
I've edited my answer to include the modified properties. As far as redirecting to another page to edit the selected products, that's a separate issue and you need to ask a separate question.
Thanks..have raised new question - stackoverflow.com/questions/27398894/…
@DreamBig, Had a quick look (no time to answer right now). No point just repeating this question - it will only get voted down and closed. Explain what you have done so far and just address the issue of passing the selected values to another method for editing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.