2017-02-27 8 views
0

私はaps.net mvcのビューから値を読み取ろうとしています。これは非常に基本的な問題のようですが、私はこれについての解決策を見つけることができませんでした。私の場合は、パラメータplaylistModel.Model.Nameが送信されないか、少なくともnullであるように見えます。簡単な作成者のトラブルシューティング

マイコントローラ:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(PlaylistViewModelDetails playlistModel) 
{ 
    if (!String.IsNullOrEmpty(playlistModel.Model.Name)) 
    { 
     //this is never called due to playlistModel.Model.Name being null. 
     return RedirectToAction("Index"); 
    } 
    return View(playlistModel); 
} 


@model Orpheus.Models.ViewModels.PlaylistViewModelDetails 
@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Model.Name, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Erstellen" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 


public class PlaylistViewModelDetails 
{ 
    public PlaylistModel Model = new PlaylistModel(); //a seperate class containing a string value, which must be read from the form 
} 

は、この問題を解決するために私を助けてくれてありがとう!

答えて

1

PlaylistViewModelDetailsにはModelのフィールドのみが含まれています。 DefaultModelBinderはプロパティのみをバインドし、フィールドはバインドしません。

public class PlaylistViewModelDetails 
{ 
    public PlaylistModel Model { get; set; } 
} 

にモデルを変更し、あなたもPlaylistModelNamePlaylistModel

public PlaylistViewModelDetails() 
{ 
    Model = new PlaylistModel(); 
} 

注意を初期化したい場合は、パラメータなしのコンストラクタを追加

も財産にする必要があります。

+0

ありがとう、それは私のためにそれを解決した:) –

関連する問題