ToggleFieldTesting

CSharp · Mvc

Model

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
	public class SampleViewModel
	{
		[Display(Name = "Use Forecast?")]
		public bool UseForecast { get; set; }
		
		[Display(Name = "Minimum Quantity")]
		public int MinimumOnHandQuantity { get; set; }
		
		[Display(Name = "Forecast Multiplier")]
		public int ForecastMultiplier { get; set; }
	}
}

View

@model HelloWorldMvcApp.SampleViewModel
@{
	Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Bootstrap 101 Template</title>

		<!-- CSS Includes -->
		<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
		
		<style type="text/css">

			.field-validation-error {
				color: #ff0000;
			}

		</style>
			
			

		<!-- JS includes -->
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
		<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
	
		<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
		<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
			
		<script type="text/javascript">
$(document).ready(function () {
	var initial = $('#UseForecast').is(':checked');
			
	if (!initial) {
         $('#MinimumOnHandQuantity').attr("disabled", "disabled")
	}
			
			
    $('#UseForecast').click(function() {
        var $this = $(this).is(':checked');

        if ($this) {
            $('#MinimumOnHandQuantity').removeAttr("disabled");
            $('#ForecastMultiplier').attr("disabled", "disabled")
        } else {
            $('#MinimumOnHandQuantity').attr("disabled", "disabled")
            $('#ForecastMultiplier').removeAttr("disabled");
        }
    });
			});
</script>
	</head>
	
	<body>
		<div class="container">
			<div class="col-md-6 col-md-offset-3">
				<h1>Toggle Field Testing</h1>
	
				@using (Html.BeginForm())
				{
					<div class="form-group">
						@Html.LabelFor(model => model.UseForecast)
						@Html.EditorFor(model => model.UseForecast, new { htmlAttributes = new { @class = "form-control" } })
						<br />
						@Html.LabelFor(model => model.MinimumOnHandQuantity)
@Html.EditorFor(model => model.MinimumOnHandQuantity, new { htmlAttributes = new { @class = "form-control" } })
						<br />
						@Html.LabelFor(model => model.ForecastMultiplier)
@Html.EditorFor(model => model.ForecastMultiplier, new { htmlAttributes = new { @class = "form-control" } })
					</div>
				
					<button type="button" class="btn btn-success submit">Submit</button>
				}
	
				<br/><br/>
			</div>
		</div>
		
	</body>
</html>

Controller

using System;
using System.Web.Mvc;
using System.Collections.Generic;

namespace HelloWorldMvcApp
{
	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			return View(new SampleViewModel());
		}


	}
}
;