南希的模型绑定

时间:2013-12-26 20:44:57

标签: c# .net nancy

出于某种原因,我无法让NancyFx绑定到我的网页模型。如果重要的话,我自己主持这个。

这是我的路线代码:

Get["/fax.html"] = p =>
{
    FaxModel model = new FaxModel();

    var foundType = processes.Where(proc => proc.GetType().ToString().Contains("FaxServer"));
    if(foundType.First() != null)
    {
        bool enabled = Boolean.Parse(WorkflowSettings.GetValue(foundType.First().GetProcessName(), "Enabled"));
        bool deleteAfterSuccess = Boolean.Parse(WorkflowSettings.GetValue(foundType.First().GetProcessName(), "DeleteWorkflowItemsAfterSuccess"));

        model.EnableFaxes = enabled;
        model.DeleteFaxes = deleteAfterSuccess;

        // Bind the data
        this.BindTo<FaxModel>(model);
    }

    return View["fax.html"];
};

这是我的模特:

[Serializable]
public class FaxModel
{
    public bool EnableFaxes { get; set; }
    public bool DeleteFaxes { get; set; }
}

现在这是我的HTML代码:

<div id="body">
  <form method="post" action="fax.html" name="fax_settings">
  <ul>
    <li>
      <input name="EnableFaxes" value="true" type="checkbox">Automated Faxing Enabled
    </li>
    <li>
      <div style="margin-left: 80px;"><input name="DeleteFaxes" value="true" type="checkbox">Delete workflow items when fax is successful</div>
    </li>
  </ul>
  <button name="Save">Save</button>
  </form>
</div>

我无法理解为什么它根本没有填充这些复选框。有人有想法吗?

1 个答案:

答案 0 :(得分:2)

您正在使用BindTo覆盖设置。删除该调用并返回带参数的视图。

this.Bindthis.BindTo用于将输入参数(查询,表单,请求主体)绑定到模型,而不是将数据绑定到视图。

Get["fax"] = p =>
{
    FaxModel model = new FaxModel();

    var foundType = processes.Where(proc => proc.GetType().ToString().Contains("FaxServer"));
    if(foundType.First() != null)
    {
        bool enabled = Boolean.Parse(WorkflowSettings.GetValue(foundType.First().GetProcessName(), "Enabled"));
        bool deleteAfterSuccess = Boolean.Parse(WorkflowSettings.GetValue(foundType.First().GetProcessName(), "DeleteWorkflowItemsAfterSuccess"));

        model.EnableFaxes = enabled;
        model.DeleteFaxes = deleteAfterSuccess;
    }

    return View["fax", model];
};

或者,就模型类遵循约定而言,您可以这样做:

return View[model];

请参阅view engine examples

另外,你的html应该使用如下的模型属性:

<input name="EnableFaxes" value=@Model.EnableFaxes type="checkbox">Automated Faxing Enabled