复选框列表ASP.NET MVC的回发问题 [英] Issue in postback of checkbox list ASP.NET MVC

查看:72
本文介绍了复选框列表ASP.NET MVC的回发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试在ASP.NET MVC中创建checkboxlist。我从下面提供的教程中尝试了它。

使用Razor在MVC 4中创建简单的复选框列表[ ^ ]



我面临一个我无法解决的问题解决。

以下是相同的代码。



  //  模型 
命名空间 MvcCheckBox.Models
{
public class CheckModel
{
public int Id { get ; set ;}
public string 名称{获取; 设置 ;}
public bool 已选中{ get ; set ;}
}
}
// 控制器
命名空间 MvcCheckBox.Controllers
{
public class HomeController:Controller
{
[HTTPGet]
public ActionResult Index()
{
var list = new 列表< CheckModel> { new CheckModel {Id = 1 ,Name = A,C hecked = false },
new CheckModel {Id = 2 ,Name = B,Checked = },};
return 查看(列表);
}
[HTTPPost]
public ActionResult Index(List< CheckModel> list)
{
< span class =code-keyword> return
查看(列表);
}
}
}



 //查看
@model List < MvcCheckBox.Models.CheckModel >
@ {
viewBag.Title =Index;
}
< h2 > 索引< / h2 > ;
@using(Html.BeginForm())
{
for(var i = 0; i < Model.Count; i ++)

< span class =code-attribute> {

< table > < tr >
< td >
@ Html.HiddenFor(it => it [i] .Id)
@ Html.HiddenFor(it => it [i]。姓名)
< / td >
< td > ;
@ Html.CheckBoxFor(it => it [i] .Checked,new {Style =vertical-align:3px})
< / td >
< / tr > < / table >
}
< 输入 id = Submit1 type = 提交 value = 提交 / >
}





问题:

我运行代码输出正是它应该是如何呈现名称和复选框。

选中复选框并单击提交后,它不显示nam e,即在回发期间复选框没有名称。应用断点后,列表显示Name属性为空。

我无法理解这个问题。

我是ASP.NET MVC的新手,所以仍然试验MVC。



谢谢&问候

Hari



我尝试过:



我不知道该尝试或搜索什么。欢迎任何帮助/建议并尝试实施。

解决方案

您好,



您是试图为复选框提供相同的ID



这是不可能的



尝试使用不同的id

Hi,

I am trying to create checkboxlist in ASP.NET MVC. I tried it from tutorial provided below
Creating Simple Checkboxlist in MVC 4 Using Razor[^]

I am facing an issue which I am not able to resolve.
Below is the code for the same.

//Model
namespace MvcCheckBox.Models
{
   public class CheckModel
   {
      public int Id{get;set;}
      public string Name{get;set;}
      public bool Checked{get;set;}
   }
}
//Controller
namespace MvcCheckBox.Controllers
{
   public class HomeController:Controller
   {
      [HTTPGet]
      public ActionResult Index()
      {
         var list=new List<CheckModel>{new CheckModel{Id=1,Name="A",Checked=false},
                                     new CheckModel{Id=2,Name="B",Checked=false},};
         return View(list);
      }
      [HTTPPost]
      public ActionResult Index(List<CheckModel> list)
      {
         return View(list);
      }
   }
}


//View
@model List<MvcCheckBox.Models.CheckModel>
@{
   viewBag.Title="Index";
}
<h2>Index</h2>
@using(Html.BeginForm())
{
   for(var i= 0;i<Model.Count;i++)

   {

      <table><tr>
         <td>
            @Html.HiddenFor(it=>it[i].Id)
            @Html.HiddenFor(it=>it[i].Name)
         </td>
         <td>
            @Html.CheckBoxFor(it=>it[i].Checked,new{Style="vertical-align:3px"})
         </td>
      </tr></table>
   }
   <input id="Submit1" type="submit" value="Submit"/>
}



Issue:
When I run the code output is exactly how it should be i.e. name and checkbox is rendered.
After checking checkbox and clicking on submit, it doesn't displays name, i.e. during postback checkbox is rendered without name. After applying breakpoint, list shows "Name" property as empty.
I am not able to understand the problem.
I'm new to ASP.NET MVC, so still experimenting with MVC.

Thanks & Regards
Hari

What I have tried:

I don't know what to try or search. Any help/suggestion are welcome and will try to implement it.

解决方案

Hi,

You are trying to give same id for the check boxes

that is not possible

try with diffrent id


这篇关于复选框列表ASP.NET MVC的回发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆