我是ASP.NET MVC的新手,我尝试创建一个简单的View来创建一个新的Server
对象。这是Server
类:
namespace MyApp.Models
{
public partial class Server
{
public Server()
{
this.Id = Guid.NewGuid();
}
[Key]
[Required]
public Guid Id { get; set; }
[Required]
[StringLength(25)]
[Display(Name="Server Name")]
public string Name { get; set; }
[Required]
[Display(Name="Domain")]
public Domain Domain { get; set; }
}
}
这是Domain
班级:
namespace MyApp.Models
{
public class Domain
{
public Domain()
{
this.Id = Guid.NewGuid();
}
[Key]
[Required]
public Guid Id { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String FullyQualifiedName { get; set; }
}
}
我在服务器的Create.cshtml中有一个DropdownListFor
域,如下所示:
@Html.DropDownListFor(m => m.Domain.Id, (SelectList)ViewBag.DomainItems, "Please select a domain", new { @class = "form-control" })
这很有效,直到我尝试提交要保存的表单。似乎表单试图将选定的Dropdown值保存到新的Domain
对象。有什么方法可以覆盖这个并检索&手动设置Domain
Server
对象?
答案 0 :(得分:0)
更改dropdownlistfor的扩展方法,如下所示
@Html.DropDownListFor(m => m.Id, (SelectList)ViewBag.DomainItems, "Please select a domain", new { @class = "form-control" })