与往常一样,你可以通过创建一个视图模型开始:
public class MyViewModel
{
public int RadioValue { get; set; }
}
然后控制器:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
// Set to some initial value
RadioValue = 1
});
}
public ActionResult CheckVisibility(int radioValue)
{
return Json(new
{
// based on the value of the radio decide whether
// the textbox should be shown
visible = radioValue == 1
}, JsonRequestBehavior.AllowGet);
}
}
最后一个强类型的视图:
$(function() {
// when one of the two radio buttons is clicked
$('.myradio').click(function() {
// prepare the request
var data = { radioValue: $(this).val() };
// url to send the AJAX request to
var url = '';
// send an AJAX request
$.getJSON(url, data, function (json) {
// when the request succeeds toggle the visibility of the textbox
// based on the JSON response returned by the server
$('#foo').toggle(json.visible);
});
});
});
Value 1: x.RadioValue, "1", new { @class ="myradio" })%>
Value 2: x.RadioValue, "2", new { @class = "myradio" })%>