简体   繁体   English

如何从控制器中的复选框分配的剃刀变量中读取数据

[英]How to read the data in a razor variable , assigned by a checkbox from a controller

My View has a list of rows for Edit , and each row has multiple checkbox . “我的视图”具有用于“编辑”的行的列表,并且每一行都有多个复选框。 So I want to index each checkbox value with its corresponding row index. 所以我想索引每个复选框值及其对应的行索引。 For this I have in view : 为此,我认为:

List<string> FilterIds =new List<string>();

. . .

<td width="200px">
                        <div class="form-group">
                            <div class=" text-left">Apply Filter(s)</div>
                            <div class="col-md-10">



                                    @foreach (var item in ViewBag.Filters)
                                    {

                                    <input type="checkbox" value="@item.FilterId" name="@FilterIds"/>@item.FilterId
                                    <text>&nbsp;</text>
                                       //FilterId[i] = FilterIds;

                                    }

And my controller looks like this : 我的控制器看起来像这样:

public ActionResult Edit([Bind(Include = "Conf_ActionTypeCTAMeta_V1_Id,Conf_CTAId,Conf_Clicklistid,ApprovedBy,DateApproved,EditedBy,DateEdited,Conf_TrackingActionTypeUnique_Id,ConfMarketid,MarketFilterId")] Conf_ActionTypeCTAMeta_V1[] conf_ActionTypeCTAMeta_V1, List<string> FilterIds)

Any quick solutions , how I can read the checked value per row ? 任何快速解决方案,如何读取每行的检查值?

You have to create a view method for control every value's checkbox and you have to use CheckboxFor for bind it with model i couldn't help correctly because i don't know well too but i had a problem like this i used many textbox with data 您必须创建一个用于控制每个值的复选框的视图方法,并且必须使用CheckboxFor将其与模型绑定,我无法正确地帮忙,因为我也不很了解,但是我遇到了这样的问题,我使用了许多文本框和数据

This is my question and answer worked at me it's can be helpful: How can i take many textboxes' value on Post in MVC 这是我的问题和解答,对我有用,这可能会有所帮助: 如何在MVC中获取Post上许多文本框的值

And if you want to examine my solution (Page2.cshtml): https://github.com/kadirkalkan/WorkCubeFormMvc 如果要检查我的解决方案(Page2.cshtml): https : //github.com/kadirkalkan/WorkCubeFormMvc

In this code has little bit mistake. 在这段代码中几乎没有错误。

when you post data view(html) page to server side that time map with action parameter and html input tag "name attribute". 当您将数据视图(html)页面发布到服务器端时,该时间映射具有操作参数和html输入标签“名称属性”。 and the name is same then assign value into param. 并且名称相同,然后将值分配给param。

replace @FilterIds to FilterIds because you define controller in "FilterIds" so. 将@FilterIds替换为FilterIds,因为您在“ FilterIds”中定义了控制器。

<input type="checkbox" value="@item.FilterId" name="FilterIds"/>@item.FilterId

not need any extra variable to store selected value. 不需要任何额外的变量来存储所选值。

Using html input tag with razor syntax the razor syntax work for display purpose. 使用带有razor语法的html input标签,可将razor语法用于显示目的。

Update Action Method : 更新操作方法:

public ActionResult Edit([Bind(Include = "Conf_ActionTypeCTAMeta_V1_Id,Conf_CTAId,Conf_Clicklistid,ApprovedBy,DateApproved,EditedBy,DateEdited,Conf_TrackingActionTypeUnique_Id,ConfMarketid,MarketFilterId")] Conf_ActionTypeCTAMeta_V1[] conf_ActionTypeCTAMeta_V1, CheckBox checkBox)

Create DTO class like :- 创建DTO类,例如:

public class CheckBox
{
  public List<string> FilterIds1 {get;set;}
  public List<string> FilterIds2 {get;set;}
  public List<string> FilterIds3 {get;set;}
} 

update name attribute 更新名称属性

 <input type="checkbox" value="@item.FilterId" name="checkBox.FilterIds1"/>@item.FilterId

    <input type="checkbox" value="@item.FilterId" name="checkBox.FilterIds2"/>@item.FilterId

    <input type="checkbox" value="@item.FilterId" name="checkBox.FilterIds3"/>@item.FilterId

if you row generate dynamic then you can use like this: 如果您生成动态行,则可以这样使用:

public ActionResult Edit([Bind(Include = "Conf_ActionTypeCTAMeta_V1_Id,Conf_CTAId,Conf_Clicklistid,ApprovedBy,DateApproved,EditedBy,DateEdited,Conf_TrackingActionTypeUnique_Id,ConfMarketid,MarketFilterId")] Conf_ActionTypeCTAMeta_V1[] conf_ActionTypeCTAMeta_V1, CheckBox[] checkBox)

DTO class is : DTO类是:

public class CheckBox
    {
      public int Id { set; get; }
      public string Name { set; get; }
      public int index { set; get; }
    } 

i is a index value 我是一个指标值

 foreach (var item in ViewBag.Data)
{
    <input type="checkbox" value="@item.Id" name="checkbox[@item.index].Id" id="checkbox[@item.index].Id"/>@item.Name  <br />
    <input type="hidden" value="@item.index"  name="checkbox[@item.index].index"/>
}

I got this done, using 2 checkboxes and sync. 我使用2个复选框和同步完成了此任务。 them with javascript. 他们用JavaScript。

      <ul>
                                    @foreach (var item in ViewBag.Filters)
                                    {

                                        <li>
                                            <input type="checkbox" id="fltrids@(i+1)@(item.FilterId)" value="@item.FilterId" name="c" + @i onclick="synch(@(i+1)@(item.FilterId))" style="width:5%; height:5%; border:5px; outline:double" /><text>&nbsp;</text>@item.FilterId
                                            <input type="checkbox" id="mtch@(i+1)@(item.FilterId)" value="@i" name="s" />
                                            <text>&nbsp;</text><text>&nbsp;</text>

                                        </li>
                                    }
                                </ul>

and the javascript is a simple function , 而javascript是一个简单的函数,

function synch(j) {
        var a = "fltrids" + j;
        var b = "mtch" + j;
        //console.log("a-" + a + "---- b-" + b);
            var x = document.getElementById(a).checked;
            if (x == true) {
                document.getElementById(b).checked = true;
            }
            else {
                document.getElementById(b).checked = false;
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM