如何在mvc3中的Controller中访问动态创建的复选框值? [英] How to access dynamically created checkbox value in Controller in mvc3?

查看:68
本文介绍了如何在mvc3中的Controller中访问动态创建的复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含复选框和提交"按钮的视图,如下所示.

I have the view that contains the checkbox and Submit button as shown below.

@using (Html.BeginForm())
    {
        <fieldset>
            <legend style="font-size: 100%; font-weight: normal">Delete</legend>
            <p> Are you sure you want to delete?</p>
            @foreach (string resource in resources)
            {
                if (resource != "")
                {
                    <input type="checkbox" name="Resources" title="@resource" value="@resource" checked="checked"/>@resource
                    <br />
                }
            }
            <br />

            @Html.HiddenFor(m => m.AttendeeListString)
        @Html.HiddenFor(m => m.ResourceListString)

            <span class="desc-text">
                <input type="submit" value="Yes" id="btnYes" />
            </span>
            <span class="desc-text">
                <input type="submit" value="No" id="btnNo" />
            </span>
        </fieldset>
    }

下面是控制器代码...

Below is the Controller code...

public ActionResult DeleteResource(RoomModel roomModel)
{
...
}

RoomModel包含其他一些数据...

RoomModel contains some other data...

现在如何访问控制器中的复选框值? 注意:当我单击提交"按钮时,我还有很多信息需要发送给Controller.有人可以提出一些解决方案吗?.

Now how can i access the checkbox value in controller? Note : I have lot more information that need to be send to Controller when i clicked on submit button... Can anybody suggest some solution....

答案:

我已将这两个属性添加到我的模型中

I have added these two property to My model

public List<SelectListItem> Resources
{
    get;
    set;
}

public string[] **SelectedResource**
{
    get;
    set;
}

我的我的视图"复选框已进行如下更新

And My view check box i have updated as follows

@foreach (var item in Model.Resources)
{
<input type="checkbox" name="**SelectedResource**" title="@item.Text" value="@item.Value" checked="checked"/>@item.Text
<br /><br />
}

然后在Controller中...

And in Controller ...

if (roomModel.SelectedResource != null)
{
    foreach (string room in roomModel.**SelectedResource**)
    {
      resourceList.Add(room);
    }
}

注意:模型中复选框的名称和属性应相同.就我而言,它是 SelectedResource

Note: The name of check box and Property in the model should be same. In my case it is SelectedResource

推荐答案

您有一些选择.最简单的是:

You have a few options. The easiest would be:

1)参数将视图模型与Resources属性绑定在一起.我建议采用这种方式,因为它是首选的MVC范例,您可以为需要捕获的任何其他字段添加属性(并且只需添加属性即可轻松利用验证).

1) Parameter bind a view model with the Resources property. I recommend this way because it's the preferred MVC paradigm, and you can just add properties for any additional fields you need to capture (and can take advantage of validation easily by just adding attributes).

定义新的视图模型:

public class MyViewModel
{
    public MyViewModel()
    {
       Resources = new List<string>();
    }

    public List<string> Resources { get; set; }

    // add properties for any additional fields you want to display and capture
}

在控制器中创建操作:

public ActionResult Submit(MyViewModel model)
{
      if (ModelState.IsValid)
      {
           // model.Resources will contain selected values
      }
      return View();   
}

2)参数直接在操作中绑定名为resources的字符串列表:

2) Parameter bind a list of strings named resources directly in the action:

public ActionResult Submit(List<string> resources)
{
      // resources will contain selected values

      return View();   

}

重要的是要注意,在问题中,视图正在创建复选框,该复选框将发送所有已检查资源的字符串值,而不是布尔值(如果您使用@Html.CheckBox帮助器,则可能会期望布尔值),该值指示每个项目是否检查与否.很好,我只是指出为什么我的答案有所不同.

It's important to note that in the question, the view is creating checkboxes that will send the string value of all checked resources, not boolean values (as you might expect if you used the @Html.CheckBox helper) indicating if each item is checked or not. That's perfectly fine, I'm just pointing out why my answer differs.

这篇关于如何在mvc3中的Controller中访问动态创建的复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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