C# 南希的模型绑定

C# 南希的模型绑定,c#,.net,nancy,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

由于某些原因,我无法让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"];
};
下面是我的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>

  • 启用自动传真
  • 传真成功时删除工作流项目
拯救

我不明白为什么它根本没有填充这些复选框。有人有主意吗?

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

this.Bind
this.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];

此外,您的html应使用如下模型属性:

<input name="EnableFaxes" value=@Model.EnableFaxes type="checkbox">Automated Faxing Enabled
已启用自动传真

这两种方法似乎都不起作用。将我的代码更改为您发布的代码。已验证数据库,以确保模型值分别为true和false,并且当我执行完整重建然后运行时,它不会显示已选中或已选中。它将无法按您的方式工作。它是一个静态html,无法替换参数。你需要一些视图引擎。请看我提供的示例链接。只要我输入引擎的数据,它就起作用了。文档并没有真正定义绑定,这让我很反感。非常感谢!
<input name="EnableFaxes" value=@Model.EnableFaxes type="checkbox">Automated Faxing Enabled