Last active
August 29, 2015 14:08
-
-
Save angularsen/ed5d5af4967b25ddb904 to your computer and use it in GitHub Desktop.
Simpler forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CreateEdit.cshtml | |
@using (Html.BeginForm("Create", "Licenses", FormMethod.Post, new {role = "form"})) | |
{ | |
@Html.AntiForgeryToken() | |
@Html.ValidationSummary(true, "", new {@class = "text-danger"}) | |
@Html.HiddenFor(m => m.App.Id) | |
<fieldset> | |
<legend>Constraints</legend> | |
@Html.EditorFor(m => m.Constraints.MaxInitialReleaseDateOfMinorVersion, "TextBox") | |
@Html.EditorFor(m => m.Constraints.MaxStartups, "DropDownListVmInt") | |
@Html.EditorFor(m => m.Constraints.MaxTimeFromFirstStartup, "DropDownListVmTimeSpan") | |
@Html.EditorFor(m => m.Constraints.MaxTimeRunningApp, "DropDownListVmTimeSpan") | |
@Html.EditorFor(m => m.Constraints.MaxVersion, "TextBox") | |
@Html.EditorFor(m => m.Constraints.ValidTo, "TextBox") | |
</fieldset> | |
// ... | |
} | |
// ConstraintsVm (Constraints property in CreateEditVm) | |
public class ConstraintsVm | |
{ | |
[Display(Name = "Time Limit from First Startup", | |
Description = "Stops working after a given time after the first startup, regardless of how much it has been run after that.")] | |
public DropDownListVm<TimeSpan> MaxTimeFromFirstStartup { get; set; } | |
// ... | |
} | |
// DropDownListVmTimeSpan.cshtml | |
@model simplicense.ViewModels.Shared.DropDownListVm<TimeSpan> | |
<div class="form-group"> | |
@Html.LabelFor(x => x, new {@class = "col-md-3 control-label"}) | |
<div class="col-md-9"> | |
@Html.DropDownListFor(x => x.Value, Model.Values, new { @class = "form-control", title = ViewData.ModelMetadata.Description }) | |
<span class="help-block">@ViewData.ModelMetadata.Description</span> | |
@Html.ValidationMessageFor(x => x, "", new {@class = "text-danger"}) | |
</div> | |
</div> | |
// TextBox.cshtml | |
@model Object | |
<div class="form-group"> | |
@Html.LabelFor(x => x, new {@class = "col-md-3 control-label"}) | |
<div class="col-md-9"> | |
@Html.TextBoxFor(x => x, new {@class = "form-control", title = ViewData.ModelMetadata.Description, placeholder = ViewData.ModelMetadata.Watermark}) | |
<span class="help-block">@ViewData.ModelMetadata.Description</span> | |
@Html.ValidationMessageFor(x => x, "", new {@class = "text-danger"}) | |
</div> | |
</div> | |
// CheckBox.cshtml | |
@model bool | |
<div class="form-group"> | |
@Html.LabelFor(x => x, new {@class = "col-md-3 control-label"}) | |
<div class="col-md-9"> | |
@Html.CheckBoxFor(x => x, new { title = ViewData.ModelMetadata.Description }) | |
<span class="help-block">@ViewData.ModelMetadata.Description</span> | |
@Html.ValidationMessageFor(x => x, "", new {@class = "text-danger"}) | |
</div> | |
</div> | |
// ControlModelBinder | |
using System; | |
using System.Web.Mvc; | |
using simplicense.ViewModels.Shared; | |
namespace simplicense.ViewModels.ModelBinders | |
{ | |
/// <remarks>Based on: http://stackoverflow.com/a/6333898/134761 </remarks> | |
public class ControlModelBinder : DefaultModelBinder | |
{ | |
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, | |
Type modelType) | |
{ | |
ValueProviderResult type = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Type"); | |
object model = null; | |
switch (type.AttemptedValue) | |
{ | |
case "textbox": | |
{ | |
model = new TextBoxVm<string>(); | |
break; | |
} | |
case "checkbox": | |
{ | |
model = new CheckBoxVm(); | |
break; | |
} | |
case "ddl": | |
{ | |
model = new DropDownListVm<string>(); | |
break; | |
} | |
default: | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
; | |
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, | |
model.GetType()); | |
return model; | |
} | |
} | |
} | |
// View models | |
public class DropDownListVm<TValue> : TextBoxVm<TValue> | |
{ | |
public DropDownListVm() | |
{ | |
} | |
public DropDownListVm(SelectList<TValue> values) | |
{ | |
Values = values; | |
Value = values.SelectedValue; | |
} | |
public DropDownListVm(string label, string name, TValue value, SelectList values) : base(label, name, value) | |
{ | |
Values = values; | |
} | |
public override string Type | |
{ | |
get { return "ddl"; } | |
} | |
public SelectList Values { get; set; } | |
} | |
public class SelectList<TValue> : SelectList | |
{ | |
public SelectList(IEnumerable<SelectItem<TValue>> items, TValue selectedValue) | |
: base(items, "Value", "Text", selectedValue) | |
{ | |
} | |
[UsedImplicitly] | |
public SelectList() : base(new List<TValue>()) | |
{ | |
} | |
public new TValue SelectedValue | |
{ | |
get { return (TValue) base.SelectedValue; } | |
} | |
} | |
public class SelectItem<TValue> | |
{ | |
[UsedImplicitly] | |
public SelectItem() | |
{ | |
} | |
public SelectItem(TValue value, string text) | |
{ | |
Value = value; | |
Text = text; | |
} | |
[UsedImplicitly] | |
public TValue Value { get; set; } | |
[UsedImplicitly] | |
public string Text { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment