2

I created a model and i am trying to build something like when i click on submit button the values should be passed to controller which looks like

[HttpPost]
// i am getting checked property but not phoneno to controller 
public ActionResult Confirm(Entity s) 
{
    return view();    
}

My View

@model MvcApplication3.Models.Entity

@using ( Html.BeginForm("Confirm", "Home", FormMethod.Post))
{
   @Html.CheckBoxFor(m=>m.checkedprop,
      new 
      { 
          @class = "myCheckBox", 
          phoneno= "1234"    //tried different ways none looks working for me 
      })

   <input type="submit" id="ConfirmButton"   />
}

Model

 public class Entity
 {
    public bool checkedprop { get; set; }
    public int phoneno { get; set; }
 }
Kyle Gobel
  • 5,530
  • 9
  • 45
  • 68
super cool
  • 6,003
  • 2
  • 30
  • 63
  • There is no HTML controls rendered for `phoneno`. – Dennis R Jul 02 '14 at 19:13
  • yes its true .! but i cant create a separate html control to do so . any suggestions on this . cheers – super cool Jul 02 '14 at 19:16
  • What about a hidden field? – Dennis R Jul 02 '14 at 19:17
  • no mate i should not use any of those i have to use `@Html.CheckBoxFor`. – super cool Jul 02 '14 at 19:19
  • 1
    By the way, phone numbers should be strings, not ints. What if the number starts with a 0? Will you ever be adding or dividing with them? What if you want to find all the phone numbers in a certain area code (ie, that start with certain characters), etc. – Pharylon Jul 02 '14 at 19:59
  • ha nice catch . well for time being and just to describe my senario i done like that :) cheers – super cool Jul 03 '14 at 05:33
  • Concatenate values using @Html.CheckBoxFor(m=>(m.checkedprop+","+m.phoneno). And split these in your controller , if you want to stick with @Html.CheckBoxFor. – Anupam Singh Jul 03 '14 at 10:12

2 Answers2

2

Your markup

@Html.HiddenFor(m=>m.phoneno)

and in your controller you can get

[HttpPost]
public ActionResult Confirm(Entity s, string phoneno) 
{
  return view();    
}
Dennis R
  • 3,195
  • 1
  • 19
  • 24
  • please look into this link you can get something i'm unable to crack it . http://stackoverflow.com/questions/9444805/how-to-specify-data-attributes-in-razor-e-g-data-externalid-23151-on-this . cheers – super cool Jul 02 '14 at 19:27
  • 2
    Well, data attributes are not included in the data that's posted with the form. There is no way you can get this value in the controller's action other than using a hidden field in your case. Of course you can get the data attribute values through jquery and post the form via AJAX if that's the approach you want to take. You can look [here](http://stackoverflow.com/questions/23385144/mvc-get-a-form-control-data-attribute-value-in-a-post-actionresult) – Dennis R Jul 02 '14 at 19:36
1

Change

@Html.CheckBoxFor(m=>m.checkedprop,new { 
        @class = "myCheckBox", 
        phoneno= "1234"    //tried different ways none looks working for me 
    })

to

@Html.CheckBoxFor(m=>m.checkedprop,new {@class = "myCheckBox"})
@{Model.phoneno = 1234}
@Html.HiddenFor(m => m.phoneno)

The reason yours didn't work is you were trying to set the phone number as an attribute of the checkbox. You need to submit that phone number as a separate field on the form. Since it's hardcoded, and the user isn't inputing it, instead of submitting it from a textbox (or whatever), you send it as a hidden field on the form.

Pharylon
  • 9,796
  • 3
  • 35
  • 59
  • i cant use hidden field . any alternate wayinside checkboxfor which sends phoneno – super cool Jul 02 '14 at 19:20
  • Why can't you use a hidden field? – Pharylon Jul 02 '14 at 19:21
  • well in reality we can but i am thinking how can it be done with what i have . – super cool Jul 02 '14 at 19:22
  • If you have a value that needs to be submitted with a form, that the user doesn't enter, this is how you do it. That what hidden fields are *for*. Now, I'm assuming you're passing a phone number to the view from the controller to populate the form? If so, you can take out the `@{Model.phoneno = "1234"}` line. – Pharylon Jul 02 '14 at 19:26
  • please look into this link you can get something i'm unable to crack it . http://stackoverflow.com/questions/9444805/how-to-specify-data-attributes-in-razor-e-g-data-externalid-23151-on-this . cheers – super cool Jul 02 '14 at 19:29
  • 2
    That link shows adding a property to a checkbox... and how does that help you? In your model, you have two properties, a bool and an int, right? Is the int a property of the bool? No, the int and the bool are siblings - both children of the same object. So since the phoneno is not the child of the checkeddownprop, it doesn't make sense for the phone number to be a property of the checkbox (which is the user-interface for the bool). That's why you submit them as two separate fields. Because they *are* two separate fields. – Pharylon Jul 02 '14 at 19:50