将RadioButton中的复杂数据传递给MVC5中的Controller

时间:2016-06-26 15:40:30

标签: c# asp.net-mvc

我无法将复杂类从单选按钮传递给Controller。有可能,我是怎么做的? 我只想用单选按钮发送一个对象。问题是,我收到错误空引用。

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>}

namespace 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");
    }
}

}

1 个答案:

答案 0 :(得分:1)

是的,你可以。你想要做的是以下(注意在你的数据类中添加一个新属性; IsSelected属性):

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; }
    }
}

..和你的观点一样

@* 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>

如果您观察到后端视图中的传入数据(控制器的Post actionResult),您将看到所有无线电都进入,但只有一个无线电将其IsSelected属性设置为true。

如果你将radiobutton更改为:

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

..所有无线电将尊重控制器中所设置的初始值,但在它们之间进行选择时,它们将无法正常运行。 (虽然可以通过微小的javascript解决)。另一个好处是IsSelected属性填充在列表的所有行中;不仅仅是选定的(真),但未选择的将带有值false(最初在控制器中设置)。