我有一個呈現視圖的Nancy服務。該視圖包含兩個模型,員工和人員。這兩個模型都有一個名爲Name的屬性。當這些值得到發佈後,似乎南希採取第一個綁定<>並將其應用於兩個具有相同名稱的屬性。有沒有辦法將輸入綁定到特定的模型,而不是我在下面做的?Nancy綁定到具有相同屬性的模型
這是模塊。
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>