C# 使用RadioButton将复杂数据传递给MVC5中的控制器
我无法将复杂类从单选按钮传递给控制器。有可能吗,我是怎么做的? 我只想用单选按钮发送一个对象。问题是,我收到错误null引用C# 使用RadioButton将复杂数据传递给MVC5中的控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我无法将复杂类从单选按钮传递给控制器。有可能吗,我是怎么做的? 我只想用单选按钮发送一个对象。问题是,我收到错误null引用 public class Model1 { public int ID { get; set; } public string Name { get; set; } public List<ComplexClass> example { get; set; } public List<string> SimpleCo
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; }
}
公共类模型1
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共列表示例{get;set;}
公共列表SimpleCollection{get;set;}
}
公共类ComplexClass
{
公共整数{get;set;}
公共字符串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>}
@使用(Html.BeginForm(“GetInfosFromView”,“Home”,FormMethod.Post)){
//@助手(模型)
@复杂(模型)
}
@助手(WebApplication1.Models.Model1 modelx){
@foreach(modelx.SimpleCollection中的var项)
{
- @item@Html.radioButton(m=>m.SimpleCollection[0],item)
}
}
@助手复合体(WebApplication1.Models.Model1集合){
@foreach(集合中的var项。示例)
{
- @item.Word | |@item.Number@Html.radioButton(m=>m.example[0],item)
}
}
命名空间WebApplication1.Controllers{
公共类HomeController:控制器
{
public ActionResult Index()
{
返回视图();
}
公众行动结果表()
{
Model1 Model1=新Model1();
model1.SimpleCollection=newlist(){“raz”、“dwa”、“trzy”};
model1.example=new List(){new ComplexClass(){Number=500,Word=“piecset”},
新的ComplexClass(){Number=1000,Word=“tysiąc”},
新的ComplexClass(){Number=100,Word=“sto”}
};
返回视图(“表单”,模型1);
}
//[HttpPost]
//公共操作结果GetInfosFromView(模型1模型2)
//{
//var模型=”;
//model=model2.SimpleCollection[0];
//ViewBag.Info=模型;
//返回视图(“索引”);
//}
[HttpPost]
公共操作结果GetInfosFromView(模型1模型2)
{
var模型=”;
model=model2。示例[0]。Number.ToString();
ViewBag.Info=模型;
返回视图(“索引”);
}
}
}是的,你可以。您需要执行以下操作(注意在数据类中添加了一个新属性;IsSelected属性):
。。所有收音机都将遵守渲染前控制器中设置的初始值,但在两者之间进行选择时,它们不会完全正常工作。(不过可以用一个小小的javascript来解决)。另一个好处是IsSelected属性填充在列表的所有行中;不仅选定的值(true),而且未选定的值都将具有false值(最初在控制器中设置) 不,不能将单选按钮(或任何其他表单控件)绑定到复杂对象。单选按钮只回发一个值。并检查为
@html.RadioButtonFor(m=>m.example[0],item)
生成的html,特别是值
属性,以了解为什么这永远不会起作用。
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");
}
}
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>
@Html.RadioButtonFor(m => m.Example[x].IsSelected, true)