【发布时间】:2016-07-15 14:38:04
【问题描述】:
我有一个呈现视图的 Nancy 服务。 View 包含两个模型,Employee 和 Person。这两个模型都有一个名为 Name 的属性。当这些值被回发时,南希似乎采用了第一个 Bind 并将其应用于具有相同名称的两个属性。除了我在下面所做的之外,还有其他方法可以将输入绑定到特定模型吗?
这是模块。
public class IndexModule : NancyModule
{
public IndexModule()
: base("/")
{
Get["/"] = parameters =>
{
var returnModel = new ReturnModel();
return View["index.cshtml", returnModel];
};
Post["/"] = parameters =>
{
var person = this.Bind<Person>(); //Name="Name"
var employee = this.Bind<Employee>(); //Name="Name", should be "empName"
return 200;
};
}
}
public class ReturnModel
{
public Person PersonModel;
public Employee EmployeeModel;
public ReturnModel()
{
PersonModel = new Person();
EmployeeModel = new Employee();
PersonModel.Name = "Name";
EmployeeModel.Name = "empName";
}
}
HTML 视图
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<NancyTestSite.Modules.ReturnModel>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<form method="POST">
<input name="@(Model.EmployeeModel.Name)" type="text" value="@(Model.EmployeeModel.Name)">
<input name="@(Model.PersonModel.Name)" type="text" value="@(Model.PersonModel.Name)">
<button type="submit">Submit</button>
</form>
【问题讨论】:
标签: c# model-binding nancy