我得到這個工作對我來說:
這裏是我的意見/政策/ Index.cshtml
@using (Html.BeginForm("Submit", "Policy")) {
<fieldset>
@Html.EditorForModel()
</fieldset>
}
這裏是我的意見/共享/ EditorTemplates/Policy.cshtml
@model Policy
<br />
<label for="IsPositive">Is positive?</label>
@Html.CheckBox("IsPositive")
<input type="text" name="PolicyText" />
<input type="submit" value="Add to Policy" title="SubmitFromReferalPolicy" />
@Html.EditorFor(a => a.PolicyLines)
這是我的觀點/共享/編輯模板/ PolicyLine.cshtml
@model PolicyLine
<br />
@this.Model.ToString()
@Html.ActionLink("Delete", "DeleteLine/" + Model.Identifier.ToString())
這裏是我的Policy.cs
public class Policy
{
public string Id { get; set; }
public List<PolicyLine> PolicyLines = new List<PolicyLine>();
public override string ToString()
{
return PolicyFormatter.FormatPolicy(this);
}
}
這裏是我的PolicyLine.cs
public class PolicyLine
{
public bool IsPositive { get; set; }
public string PolicyText { get; set; }
public Guid Identifier { get; set; }
public override string ToString()
{
return PolicyFormatter.FormatPolicyLine(this);
}
}
這裏是PolicyController.cs我add方法
[HttpPost]
public ActionResult Submit(PolicyLine submitted)
{
Policy saveMe = Policy.GetPolicyFromUserName(UserName);
submitted.Identifier = Guid.NewGuid();
saveMe.PolicyLines.Add(submitted);
Store.Write(saveMe);
return RedirectToAction("Index");
}
這裏是我的刪除方法from PolicyController.cs
public ActionResult DeleteLine(Guid identifier)
{
Policy saveMe = Policy.GetPolicyFromUserName(UserName);
PolicyLine removeMe = saveMe.PolicyLines.Find(p => p.Identifier == identifier);
saveMe.PolicyLines.Remove(removeMe);
Store.Write(saveMe);
return RedirectToAction("Index");
}