In the view I have a lot of RadioButtons
and the id is the unique for each one. when user choose one of those in controller I would like to take the ID
and VALUE
how i can do that.... i try with formCollection
but in here i just can take value...
@Html.RadioButton("BookType", "1", new { id = "100" })
<br>
Above code generates Below code
<br/>
< input id="100" name="BookType" type="radio" value="1" >
and the question is how can I take the ID and VALUE by 'POST'
Action in the control.
I suggest you to use view model. Please see this example bellow:
Now you need to create editor template for
TheOption
model. It's simply by create folder named EditorTemplates under ~\Views\Shared\ folder. Add new view as the editor template. Name this editor template to match with the model name (TheOption
).Here's the content of ~\Views\Shared\EditorTemplates\TheOption.cshtml:
Now go to your main view (Index.cshtml) and simply put this code:
Done! Hope this helps :)
You can write code in Post Action as follow:
You don't, that's not how form controls work.
When you submit a form, the
name
andvalue
are submitted, except for scenarios likecheckbox
andradio
inputs.Only when a
checkbox
ischecked
, or aradio input
ischecked
are thename/value
posted.You need to re-think what you're trying to achieve. If you can give some more information what you want to achieve I'm sure I or anyone else can help further.