c# 4.0 - I can not get values form RadioButtonFor List in asp.net mvc -
i working on project have view different layout on mobile view , desktop view. solution creating on view 2 tables define different elements each view. bind data same model. please take @ code below:
view
<table class="pc" style="border: 1px solid #eeecdc"> <tbody> <tr> <td> @html.radiobuttonfor(m => model.selectedoption, 1) </td> <td> @html.radiobuttonfor(m => model.selectedoption, 2) </td> </tr> </tbody> </table> <table class="mb" style="border: 1px solid #eeecdc"> <tbody> <tr> <td> @html.radiobuttonfor(m => model.selectedoption, 1, new { @name = "selectedoption_mb" }) </td> <td> @html.radiobuttonfor(m => model.selectedoption, 2, new { @name = "selectedoption_mb" }) </td> </tr> </tbody> </table>
model
public class testviewmodel { public testviewmodel(string pagename) { } /// <summary> /// gets or sets answer scales. /// </summary> /// <value>the answer scales.</value> public int selectedoption { get; set; } }
controller
public partial class testcontroller { [httppost] [modelstatetotempdata] public virtual actionresult testsave(testviewmodel model) { var value= model.selectedoption; } }
result
with desktop view : can value.
with mobile view : can not value ( it's null).
please me value mobile view. don't know have done wrong.
please take @ attachment pc , mobile view it's reason have define both html on 1 view.
thank much!
the main reason name of radio button on mobile view not correct , it's not matched , can not set defaultmodelbinder
@html.radiobuttonfor(m => model.selectedoption, 1, new { @name = "selectedoption_mb" })
i totally understand want value httpget
method. data shown both mobile , desktop views.
in case, if want define both view different element on 1 view
. should define more properties mobile view model
. please refer code below:
model
public class testviewmodel { public int? selectedoption { get; set; } public int? selectedoption_mb { get; set; } }
view
<table class="mb" style="border: 1px solid #eeecdc"> <tbody> <tr> <td> @html.radiobuttonfor(m => model.selectedoption_mb, 1) </td> <td> @html.radiobuttonfor(m => model.selectedoption_mb, 2) </td> </tr>
controller
var value= model.selectedoption!=nul? model.selectedoption:model.selectedoption_mb!=null? model.selectedoption_mb:null;
hopefully, helpful.
Comments
Post a Comment