2017-03-14 11 views
1

これで文字列を取得できますが、クラスを取得しようとすると例外エラーが発生します。私はすでに値を設定しようとしたASP.Net MVCのビューからコントローラにクラスの値を渡す方法

Customer = new Customer() 
{ 

    FirstName = Request.Form["FirstName"], 
    LastName = Request.Form["LastName "], 
    BillingAddress = new Address 
    { 
     StreetAddress1 = Request.Form["StreetAddress1"], 

    } 

は、それが正常に保存されますが、私は道occurs.By値例外エラーからデータを取得していたとき、私はSDKからモデルを使用しています。あなたは

表示形式ありがとう

@model WebApplication16.Model.ParentModel 

@{ 
ViewBag.Title = "Create"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 



@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>ParentModel</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 

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

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

     <div class="form-group"> 
      @Html.LabelFor(model => model.Customer.BillingAddress.StreetAddress1, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customer.BillingAddress.StreetAddress1, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customer.BillingAddress.StreetAddress1, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Customer.BillingAddress.StreetAddress2, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customer.BillingAddress.StreetAddress2, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customer.BillingAddress.StreetAddress2, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Customer.BillingAddress.City, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customer.BillingAddress.City, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customer.BillingAddress.City, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Customer.BillingAddress.StateCode, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customer.BillingAddress.StateCode, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customer.BillingAddress.StateCode, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Customer.BillingAddress.Country, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customer.BillingAddress.Country, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customer.BillingAddress.Country, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Customer.BillingAddress.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customer.BillingAddress.ZipCode, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customer.BillingAddress.ZipCode, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

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


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

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

      <div class="form-group"> 
       @Html.LabelFor(model => model.Payment.Amount, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Payment.Amount, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Payment.Amount, "", new { @class = "text-danger" }) 
       </div> 
      </div> 
      <div class="form-group"> 
       @Html.LabelFor(model => model.Payment.Cvv, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Payment.Cvv, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Payment.Cvv, "", new { @class = "text-danger" }) 
       </div> 
      </div> 






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

<div> 
@Html.ActionLink("Back to List", "Index") 
</div> 
+2

なぜRequest.Formを使用していますか? HTMLヘルパーコントロールを使用することができます。例外は何ですか? – Berkay

+1

最初に –

+0

asp.net mvcのbaiscsを読んでくださいそれはヌル値を得ています – Woshooo

答えて

3

単純な答え:あなたは、フォームで複雑なデータ型を送信することはできません。

この例では、 "StreetAddress1"を文字列( "FirstName"と "LastName"と一緒に)として送信し、コントローラにCustomerオブジェクトを作成することができます。

ビューのためのコード:

@model Customer 

<div> 
    @using (Html.BeginForm("Action", "Controller", FormMethod.Post)) 
    { 
     @Html.TextBoxFor(m => m.FirstName)<br /> 
     @Html.TextBoxFor(m => m.LastName)<br /> 
     @Html.TextBoxFor(m => m.BillingAddress)<br /> 
     <button type="submit">Save</button> 
    } 
</div> 


あなたは、このアプローチに従えば、MVCはすでにあなたのためのCustomerオブジェクトシリアライズ:あなたコントローラーに

[HttpPost] 
public ActionResult Action(Customer c) 
{ 
    string FullName = c.FirstName + " " + c.LastName 
} 
+0

あなたは私にそれのための適切なフォーマットを表示できますか? – Woshooo

+0

デバッグ時にコード例 – NotTelling

+0

を追加しましたが、それでも値はありません – Woshooo

0

使用FormCollectionクラス。これウィルすべての投稿された値を使用します。次にキーFromCollectionキーで値を抽出できます。FormCollectionから取得した値に基づいて新しいオブジェクトを作成し、単にFromCollectionパラメータを追加します

public ActionResult (FormCollection F) 
{ 

} 
+0

これは既に試してみましたが、ヌルエラーが発生しました – Woshooo

+0

おそらくあなたが間違ったキーを使用してデータを取得する...コントローラの中でFormCollection FのAllKeysプロップを取得し、デバッグブレークポイントを設定する..あなたはすべてのキーをFで保存し、一度あなたが抽出できるキーの名前を知るキーによる値 –

+0

はい、私は既にデータを取得するためにキーをもう一度チェックし、それをデバッグしようとします – Woshooo

関連する問題