MVC半复杂模型绑定其他一半总是为空

时间:2013-03-06 19:54:26

标签: asp.net asp.net-mvc-4 model-binding

更新<!/强>

好的,我现在已经测试了根据您发送的表单创建两个动作结果。

其中一个视图始终正确绑定登录模型。另一个没有绑定总是返回null。

模型的结构如下

public class RegisterAndLogin
{
    public LogOnModel Login { get; set; }
    public interested_users Register { get; set; }
}

//This model never any values after HTTP post
public class interested_users
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }
    [Required]
    [Display(Name = "UserType")]
    public string UserType { get; set; }    
    [Display(Name = "Date Inserted")]
    public DateTime DateInserted { get; set; }   
    [Display(Name = "IP")]
    public string IpAdress { get; set; }
    public List<SelectListItem> SelectItems { get; set; }
}

视图

@model MvcApplication2.Models.ViewModel.RegisterAndLogin

@{
ViewBag.Title = "Start by filling in your email";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">       </script> 
<h2 class="title">Sign up!</h2>

@using (Html.BeginForm("Index","Home", FormMethod.Post))
{   

 <div class="form-item">
         @Html.EditorFor(model => model.Register.Email,null,"emailbox",null)
        @Html.ValidationMessageFor(model => model.Register.Email)
    </div>
 <div class="form-item">
     @Html.DropDownListFor(model => model.Register.UserType,Model.Register.SelectItems, new { @class = "emailbox" })
    </div>
 <input type="submit" name="Register"  value="Register" class="form-submit" />
 }

已更新!

我在主视图中加载了两个局部视图。

它们是上面的那个,另一个是部分登录视图。

Somethings fishy ..如果我告诉NOT WORKING视图 @using(Html.BeginForm())

如果没有HTTP,它不会回到ActionReslut,它会再次转到HTTP ..这告诉我另一种形式(登录表单)正在做一些我不知道的事情?

1 个答案:

答案 0 :(得分:0)

试试吧

@Html.EditorFor(model => model.Register.Email,"emailbox")