2016-07-12 46 views
2

每当我提交表单时,传递给控制器​​的模型都是NULL。我花了很多年看这个。我想我在这里错过了一些基本的东西。模型在表单发布到控制器时始终为NULL

@model VisitorPortal.Models.ReinviteVisitorModel 
@using (Html.BeginForm("CreateMeeting", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h3>Reinvitation Details</h3> 
    <div>The information entered below will be sent to the visitors email address @Model.Info.Email</div> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.NewMeeting.Title, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.NewMeeting.Title, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.NewMeeting.StartTime, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.NewMeeting.StartTime, new { @class = "datetimepicker form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.NewMeeting.EndTime, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.NewMeeting.EndTime, new { @class = "datetimepicker form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      @Html.HiddenFor(m => m.NewMeeting.SubjectId, new { @Value = Model.Info.SubjectId }) 
      <input type="submit" class="btn btn-default" value="Send Invite" /> 
     </div> 
    </div> 
} 

的型号是:

public class Meeting 
{ 
    [Key] 
    public int Id { get; set; } 

    public string SubjectId { get; set; } 

    [Required] 
    [Display(Name = "Reason for invitation")] 
    public string Title { get; set; } 

    [Required] 
    [Display(Name = "Start Time")] 
    [DataType(DataType.Time)] 
    public DateTime StartTime { get; set; } 

    [Required] 
    [Display(Name = "End Time")] 
    [DataType(DataType.Time)] 
    public DateTime EndTime { get; set; } 

    public string HostEmail { get; set; } 

    public string HostMobile { get; set; }  
} 

public class MeetingsDBContext: DbContext 
{ 
    public DbSet<Meeting> Meetings { get; set; } 
} 

public class ReinviteVisitorModel 
{ 
    public Visitor Info; 
    public Meeting NewMeeting; 
    public List<Meeting> Meetings; 
} 

控制器中的操作是:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult CreateMeeting(Meeting meeting) 
    { 
     return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = meeting.SubjectId }); 
    } 

我在模型领域如身份证,我很期待数据库来填充这点我是将在动作CreateMeeting()中写入。模型中的所有字段都必须在表单中使用吗?

+0

你重定向到另一个动作'ReinviteVisitor2',PLZ添加其代码。顺便说一下,视图的文件名是什么? – akazemis

+0

你传递的模型和你使用的模型是不同的,几乎为什么它返回null。 – denchu

回答

5

在您的视图模型是typeof运算ReinviteVisitorModel这意味着POST方法的签名,因为你发布ReinviteVisitorModel

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult CreateMeeting(ReinviteVisitorModel model) 

或者你可以使用的BindAttributePrefix财产剥离从名称NewMeeting前缀必须匹配的表单控制你的发布。

public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model) 

边注:从隐藏的输入删除new { @Value = Model.Info.SubjectId },而是设置NewMeeting.SubjectId值在GET方法传递模型到视图之前。

+0

确定了第一部分的工作。但是,如果我按照您所描述的方式移动隐藏字段,则传递给CreateMeeting操作时它将为空(Meeting.SubjectId)。我认为该表单创建了一个新的会议对象? – rukiman

+0

我没有说要移动隐藏的输入。只需使用'@ Html.HiddenFor(m => m.NewMeeting.SubjectId)',但是在GET方法中设置它的值,以便获得真正的双向模型绑定,例如'NewMeeting.SubjectId = Info.SubjectId;'(通过设置'value'属性覆盖HtmlHelper方法的功能的不好的做法) –

+0

好吧,这是有道理的。非常感谢。 – rukiman

1

你需要传递ReinviteVisitorModel模型在Action

更新你的行动与此:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult CreateMeeting(ReinviteVisitorModel model) 
{ 
    return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = model.NewMeeting.SubjectId }); 
} 
相关问题