c# - MVC razor partial view radio button selection for each row in model -


i have partial view displays viewmodel list containing 4 data columns. want have user select 1 row , press button post action. view model looks like:

public class viewmodelselection {     public long selecteditem { get; set; }     public list<viewmodelfromdb> choices { get; set; } } 

the partial view looks like:

@model myapp.viewmodels.viewmodelselection <h4>title</h4> @using (html.beginform()) {     @html.antiforgerytoken()      <table>         <tr>             <th></th>             <th>name</th>             <th>measurea</th>             <th>measureb</th>         </tr>         @foreach (var item in model.choices)         {             <tr>                 <td>                     @html.radiobutton(item.id, model.selecteditem)                 </td>                 <td>                     @html.displayfor(modelitem => item.name)                 </td>                 <td>                     @html.displayfor(modelitem => item.measurea)                 </td>                 <td>                     @html.displayfor(modelitem => item.measureb)                 </td>             </tr>         }     </table>      <input type="submit" value="save" class="" /> } 

i'd selecteditem property of viewmodel set id field of selected row. suggestions appreciated.

your creating radio buttons different names, un grouped (i.e. can select of them) , have no relationship model. change radio button to

@html.radiobuttonfor(m => m.selecteditem, item.id, new { id = "" }) 

if post controller method parameter int selecteditem selecteditem contain id of selected choice. alternatively parameter viewmodelselection model in case model.selecteditem contain value.

side note: new { id = "" } removes inputs id attribute ensure have valid html (no duplicate id attributes)


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -