HTTPPost操作中的多个模型返回null

时间:2015-04-15 09:53:27

标签: entity-framework asp.net-mvc-5

我有一个名为“Request”的模型,其中还有三个模型,如下所示

public partial class Request
{
    public long RequestID { get; set; }
    public System.DateTime CreationDate { get; set; }
    public string RequesterName { get; set; }
    public string RequesterPhone { get; set; }       

    public virtual AccessLevels AccessLevels { get; set; }
    public virtual Department Department { get; set; }               
    public virtual Project Project { get; set; }
}

项目模型

public partial class Project
{
    public long RequestID { get; set; }
    public string ProjectName { get; set; }        

    public virtual Request Request { get; set; }
}

部门模型

public partial class Department
{
    public string DepartmentName { get; set; }        
    public string DepartmentId { get; set; }        
    public long RequestID { get; set; }

    public virtual Request Request { get; set; }
}

AccessLevels模型

public partial class AccessLevels
{
    public string AccessId { get; set; }        
    public string AccessName { get; set; }        
    public long RequestID { get; set; }

    public virtual Request Request { get; set; }
}

控制器操作

[HttpGet]
    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Request request, [Bind(Exclude = "RequestID")]Department department, [Bind(Exclude = "RequestID")]Project project, [Bind(Exclude = "RequestID")]AccessLevels accesslevels)
    {}

查看代码

    <div class="form-group">
        @Html.Label("New", htmlAttributes: new { @class ="control-label col-md-3"})
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CreationDate, htmlAttributes: new { @class = "control-label col-md-3" })
        <div class="col-md-5">
           @Html.Label(@DateTime.Now.ToShortDateString(), htmlAttributes: new { @style = "padding-top:7px" })  
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.RequesterName, htmlAttributes: new { @class = "control-label col-md-3" })
        <div class="col-md-5">
            @Html.Label(@System.Security.Principal.WindowsIdentity.GetCurrent().Name, htmlAttributes: new { @style = "padding-top:7px" })                
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.RequesterPhone, htmlAttributes: new { @class = "control-label col-md-3" })
        <div class="col-md-5">
            @Html.EditorFor(model => model.RequesterPhone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.RequesterPhone, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-5">
            @Html.EditorFor(model => model.Department)
        </div>
    </div>

    <div class="form-group">           
        <div class="col-md-5">
            @Html.EditorFor(model=>model.Project)
        </div>
    </div>        

    <div class="form-group">
        <div class="col-md-5">
            @Html.EditorFor(model => model.AccessLevels)
        </div>
    </div>

当提交时发生HTTPPost时,模型Project和AccessLevels始终为null。只有模型Request和Department才能获得值。如果我在HTTPPost方法中更改模型的顺序,那么我得到前两个参数的值,其他两个变为空。

请让我知道问题出在哪里

0 个答案:

没有答案