2016-07-12 4 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 }); 
    } 

私は私があった移入するためのデータベースを期待していなIDとしてモデル内のフィールドを持っていますCreateMeeting()アクションを記述します。モデル内のすべてのフィールドをフォームで使用する必要がありますか?

+0

'ReinviteVisitor2'は、plzはそのコードを追加します。ところで、ビューのファイル名は何ですか? – akazemis

+0

あなたが渡したモデルと使用したモデルは異なっています。 – denchu

答えて

5

あなたのビュー内のモデルはPOSTメソッドのシグネチャは、あなたの代わりにあなたが名前からNewMeetingプレフィックスを除去するためにBindAttributePrefixプロパティを使用することができますReinviteVisitorModel

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

を掲示するので、一致しなければならないことを意味するtypeof演算ReinviteVisitorModelですあなたが投稿しているフォームコントロールの

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

サイドノート:隠し入力からnew { @Value = Model.Info.SubjectId }を削除し、代わりにあなたがビューにモデルを渡す前に、GETメソッドでNewMeeting.SubjectIdの値を設定します。

+0

OKが最初の部分を稼働させました。しかし、あなたが説明したように私が隠しフィールドを移動すると、CreateMeetingアクションに渡されるときにnull(Meeting.SubjectId)になります。フォームは新しいMeetingオブジェクトを作成すると思いますか? – rukiman

+0

私は隠された入力を移動すると言っていませんでした。 '@ H​​tml.HiddenFor(m => m.NewMeeting.SubjectId)'を使用しますが、GETメソッドでその値を設定すると、真の双方向モデルバインディングを得ることができます。 'NewMeeting.SubjectId = Info.SubjectId;'( 'value'属性を設定することによって' HtmlHelper'メソッドの機能を無効にする悪い習慣) –

+0

それは意味があります。ありがとう、ありがとう。 – rukiman

1

あなたはこれであなたのAction

アップデートであなたのアクションをReinviteVisitorModelモデルを渡す必要があります:あなたは別のアクションにリダイレクトしている

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult CreateMeeting(ReinviteVisitorModel model) 
{ 
    return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = model.NewMeeting.SubjectId }); 
} 
関連する問題