Skip to content

Instantly share code, notes, and snippets.

@ArnisL
Created December 1, 2011 12:23

Revisions

  1. Arnis Lapsa created this gist Dec 1, 2011.
    80 changes: 80 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    //view
    @model StateIneligibilityModel
    @Html.Js("ineligibilities/state")
    @using (Html.BeginForm("State", "Ineligibilities", FormMethod.Post)){
    <fieldset class="state-ineligibility">
    <legend>State ineligibility:</legend>
    @Html.HiddenFor(x=>x.Project)
    <table>
    <tr>
    <td>@Html.LabelFor(x=>x.IneligibleCosts)</td>
    <td>@Html.EditorFor(x=>x.IneligibleCosts)</td>
    </tr>
    <tr>
    <td>@Html.LabelFor(x=>x.Type)</td>
    <td>@Html.EditorFor(x=>x.Type)</td>
    </tr>
    <tr>
    <td>@Html.LabelFor(x=>x.Partner)</td>
    <td class="partner">@Html.EditorFor(x=>x.Partner)</td>
    </tr>
    <tr>
    <td>@Html.LabelFor(x=>x.DiscoveredBy)</td>
    <td>@Html.EditorFor(x=>x.DiscoveredBy)</td>
    </tr>
    <tr>
    <td>@Html.LabelFor(x=>x.Explanation)</td>
    <td class="explanation">@Html.EditorFor(x=>x.Explanation)</td>
    </tr>
    </table>
    <input type="submit" value="State"/>
    </fieldset>
    }

    //view model (using MvcExtensions for metadata configuration)
    public class StateIneligibilityModel{
    public decimal? IneligibleCosts { get; set; }
    public IneligibilityType Type { get; set; }
    public string Explanation { get; set; }
    public int Project { get; set; }
    public string Partner { get; set; }
    public DiscovererInstitution DiscoveredBy { get; set; }
    public Ineligibility AsIneligibility(){
    return new Ineligibility(IneligibleCosts.Value,Type,Explanation,DiscoveredBy,Partner);
    }
    }
    public class StateIneligibilityMeta:Meta<StateIneligibilityModel>{
    public StateIneligibilityMeta(){
    Configure(x=>x.IneligibleCosts)
    .Required()
    .DisplayName("Ineligible costs");
    Configure(x=>x.Explanation)
    .AsMultilineText()
    .Required();
    Configure(x=>x.DiscoveredBy)
    .DisplayName("Discovered by")
    .Required()
    .AsDropDownList("discovererInstitutions");
    Configure(x=>x.Partner)
    .Required()
    .AsDropDownList("partners");
    Configure(x=>x.Type)
    .Required()
    .AsDropDownList("ineligibilityTypes");
    }
    }
    }

    //controller actions
    [HttpGet]
    public ActionResult State(int project){
    return View(new StateIneligibilityModel {Project=project});
    }
    [HttpPost]
    public ActionResult State(StateIneligibilityModel m){
    var p=s.Load<Project>(m.Project);
    return Do(()=>{
    p.Ineligibilities.StateIneligibility(m.AsIneligibility());
    return ToProject(m.Project)();},()=>View(m));
    //that Do() is a bit awkward way to ensure validation and some other stuff. it's not relevant
    }