由于某些原因,我无法让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>我不明白为什么它根本不填充这些复选框。有谁有主意吗?
发布于 2013-12-26 20:53:46
您正在用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 Enabledhttps://stackoverflow.com/questions/20791010
复制相似问题