asp.net mvc 3 - Get radiobutton selected according to value in database using mvc3 -


i have radiobutton list in create view.it generated along checkbox when selecting dropdown.

@model ienumerable<admin.models.viewmodel> @foreach (var item in model) {     <label>            @html.checkbox("user", item.selected, new { @id = "user" + item.value, @value = item.value })             @item.text     </label>     <label>         @html.radiobutton("rdnuser" + item.value.trimstart(), 1, item.isselected,new { @id = "rdnuser"})primary                        </label>     <label>        @html.radiobutton("rdnuser" + item.value.trimstart(), 2,item.isselected, new { @id = "rdnuser"})secondary     </label> } 

iam saving value of radiobutton field usertype in table usermapping.

when click on edit link,i want radiobutton selected according database value..

created viewmodel take values in checkbox , radiobutton.viewpartial viewmodel is:-

public class viewpartial : system.web.mvc.selectlistitem     {         public int values { get; set; }         public bool isselected { get; set; }     } 

the query is:-

var query = (from u in usermapping                                 u.userid == id && u.active == 1                                 join f in financial on u.financialid equals f.financialid                               c                                 d in c.defaultifempty()                                 select new viewpartialifc                                 {                                     text = d.finame,                                     value = sqlfunctions.stringconvert((double)d.financialid),                                     selected = d.financialid == u.financialid ? true : false,                                     values = u.usertype,                                   //isselected=???                                 }).distinct().tolist(); 

what changes should make in query radiobutton selected..

this way can create list of users , have option selected them (checkbox) , selected particular role (radiobuttons):

viewmodel:

public class adminviewmodel {   public int id { get; set; }   public string name { get; set; }   public bool isselected { get; set; }   public int radiovalue { get; set; } } 

controller:

public class homecontroller : controller {   public actionresult index()   {     var data = generateviews();      return view(data);    }    [httppost]   public actionresult index(ilist<adminviewmodel> data)   {     var selectedviews = data.where(d => d.isselected == true);      foreach (var selected in selectedviews)     {       //selected.id;       //selected.radiovalue;     }      system.diagnostics.debugger.break();      return view(data);   }    private ilist<adminviewmodel> generateviews()   {     var output = new list<adminviewmodel>();     var rand = new random();      (var count = 1; count <= 10; ++count)     {       var newview = new adminviewmodel();        newview.id = count;       newview.name = "name " + count.tostring();       newview.isselected = false;       newview.radiovalue = rand.next(1, 3);        output.add(newview);     }      return output;   } } 

view:

@model ilist<webmvc3.controllers.adminviewmodel>  <h2>index</h2>  @using (html.beginform()) {    //foreach (var item in model)   for(var index = 0; index < model.count; ++index)   {     <div>       @html.hiddenfor(m => model[index].id)       <label>         @html.checkboxfor(m => model[index].isselected, model[index].isselected)         @model[index].name       </label>       <label>         @html.radiobuttonfor(m => model[index].radiovalue, 1)          primary       </label>       <label>         @html.radiobuttonfor(m => model[index].radiovalue, 2)          secondary       </label>     </div>   }   <input type="submit" value="save" />   } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -