2016-06-26 7 views
1

ラジオボタンからコントローラに複雑なクラスを渡すことはできません。それは可能ですか、私はそれをやっていますか? ラジオボタンを使ってオブジェクトを送信したいだけです。問題は、エラーnull参照を受け取ることです。ここでRadioButtonで複雑なデータをMVC5のコントローラに渡す

public class Model1 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public List<ComplexClass> example { get; set; } 
    public List<string> SimpleCollection { get; set; } 
} 

public class ComplexClass 
{ 
    public int numerek { get; set; } 
    public string slowo { get; set; } 
} 

ビューは

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

//@helper(Model) 
@Complex(Model) 

<input type="submit" value="Submit" />} 


@helper helper(WebApplication1.Models.Model1 modelx){ 
<ul> 
    @foreach (var item in modelx.SimpleCollection) 
    { 
     <li>@item @Html.RadioButtonFor(m => m.SimpleCollection[0], item)</li> 
    } 
</ul>} 


@helper Complex(WebApplication1.Models.Model1 collection){ 
<ul> 
    @foreach (var item in collection.example) 
    { 
     <li> @item.Word || @item.Number @Html.RadioButtonFor(m => m.example[0], item)</li> 
    } 
</ul>} 

名前空間WebApplication1.Controllers { パブリッククラスにHomeControllerです:コントローラ {

public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult Form() 
    { 
     Model1 model1 = new Model1(); 
     model1.SimpleCollection = new List<string>(){"raz", "dwa", "trzy"}; 
     model1.example = new List<ComplexClass>() {new ComplexClass(){ Number=500, Word="piecset"}, 
                new ComplexClass(){ Number=1000, Word="tysiąc"}, 
                new ComplexClass(){ Number=100, Word="sto"} 
     }; 

     return View("Form", model1); 
    } 

    //[HttpPost] 
    //public ActionResult GetInfosFromView(Model1 model2) 
    //{ 

    // var model = ""; 
    // model = model2.SimpleCollection[0]; 

    // ViewBag.Info = model; 

    // return View("Index"); 
    //} 

    [HttpPost] 
    public ActionResult GetInfosFromView(Model1 model2) 
    { 

     var model = ""; 
     model = model2.example[0].Number.ToString(); 

     ViewBag.Info = model; 

     return View("Index"); 
    } 
} 

}

+0

いいえ、ラジオボタン(または他のフォームコントロール)を複合オブジェクトにバインドすることはできません。ラジオボタンは1つの値だけを返信します。 '@ H​​tml.RadioButtonFor(m => m.example [0]、item)'のために生成したhtmlを調べます - 特にこれがうまくいかない理由を理解する 'value'属性。 –

答えて

1

はいすることができます。あなたが観察した場合、この

@* This is the your POCO class, in this case is placed inside the controller itself; hence this namespace.*@ 
@model Custor.Controllers.radiobuttontest.Model1 

@{ Layout = null; } 
<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>The test controller view</title> 
</head> 
<body> 
    @using (Html.BeginForm()) 
    { 
     for (int x = 0; x < Model.Example.Count; x++) 
     { 
      <label> 
       <input name="Example[0].IsSelected" id="Example[@x].IsSelected" type="radio" value="true"> 
       @Model.Example[x].Word 
      </label> 
      @Html.HiddenFor(m => m.Example[x].Word) 
      @Html.HiddenFor(m => m.Example[x].Number) 
     } 
     <p><input type="submit" value="Post the selection" class="btn btn-default" /></p> 
    } 
</body> 
</html> 

よう

using System; 
using System.Collections.Generic; 
using System.Web.Mvc; 

namespace Custor.Controllers.radiobuttontest 
{ 
    public class radiobuttontestController : Controller 
    { 
     [HttpGet] 
     public ActionResult Index() 
     { 
      // instantiating the POCO class 
      Model1 TestData = new Model1(); 
      // Populating the instantiated class 
      TestData.ID = 1; 
      TestData.Name = "IncomingName"; 
      TestData.SimpleCollection = new List<string>() { "raz", "dwa", "trzy" }; 
      TestData.Example = new List<ComplexClass>() { 
       new ComplexClass(){ Number=500, Word="piecset", IsSelected=true}, 
       new ComplexClass(){ Number=1000, Word="tysiąc", IsSelected=false}, 
       new ComplexClass(){ Number=100, Word="sto", IsSelected=false} 
      }; 
      return View("Index",TestData); 
     } 

     // You can experiment with the binding here, to leave out or include as many fields as you like from the data model bound to the ActionResult 
     [HttpPost] 
     public ActionResult Index([Bind(Include = "ID,Name,Example,SimpleCollection")] Model1 IncomingSelection) 
     { 
      string selectedname = IncomingSelection.Name; // put a breakpoint on this line, to study the Debug > Windows > Locals output, and observe that the values are successfully posted. 
      return View("Index", IncomingSelection); // post the incoming data right back to the view again 
     } 
    } 

    public class Model1 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public List<ComplexClass> Example { get; set; } 
     public List<string> SimpleCollection { get; set; } 
    } 
    public class ComplexClass 
    { 
     public int Number { get; set; } 
     public string Word { get; set; } 
     public Nullable<bool> IsSelected { get; set; } 
    } 
} 

...とあなたのビュー:;やりたいこと(IsSelectedプロパティデータクラスの新しいプロパティの追加を注意してください)以下は、バックエンドのビュー(コントローラのPost actionResult)からの着信データは、すべての無線が入っていることがわかりますが、選択したものだけがIsSelectedプロパティがtrueに設定されています。

そして、あなたはこれにラジオボタンを変更した場合:

@Html.RadioButtonFor(m => m.Example[x].IsSelected, true) 

が...すべての無線がレンダリングされたbeforコントローラに設定された初期値を尊重しますが、それらの間で選択するとき、彼らは完全に正常に機能しません。 (しかし、小さなjavascriptで解決することができます)。もう一つの良いことは、リストのすべての行にIsSelectedプロパティが設定されていることです。選択されたものだけではなく(true)、選択されていないものはfalseになります(コントローラで最初に設定されます)。

関連する問題