Question

For some reason I cannot get NancyFx to bind to my model for a web page. I am self hosting with this if that matters.

Here is my route code:

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"];
};

Here is my model:

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

Now here is my HTML code:

<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>

I am unable to see why it is not populating these checkboxes at all. Anybody have an idea?

Was it helpful?

Solution

You are overwriting the settings with BindTo. Remove that call and return a view with parameter.

this.Bind and this.BindTo are used to bind input parameters (query, form, request body) to a model, not to bind data to a view.

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];
};

Or, as far as your model class follows the convention, you can just do:

return View[model];

See the view engine examples.

Also, your html should use the model properties like this:

<input name="EnableFaxes" value=@Model.EnableFaxes type="checkbox">Automated Faxing Enabled
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top