ASP.Net MVC 3 - 的CheckBoxList - 需要一些建议 [英] ASP.Net MVC 3 - CheckBoxList - Need some suggestions

查看:102
本文介绍了ASP.Net MVC 3 - 的CheckBoxList - 需要一些建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新ASP.Net MVC(和剃刀),我有几个问题。

I am pretty new to ASP.Net MVC (and razor) and I have a few questions.

1)

我创建的HTML扩展来创建如下一个复选框列表:

I created an HTML extension to create a check box list as below:

public static HtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, List<InputItemInfo> ItemInfo)
        {
            if (String.IsNullOrEmpty(name))
                throw new ArgumentException("The argument must have a value", "name");
            if (ItemInfo == null)
                throw new ArgumentNullException("ItemInfo");
            if (ItemInfo.Count < 1)
                throw new ArgumentException("The list must contain at least one value", "ItemInfo");

            StringBuilder sb = new StringBuilder();

            ItemInfo.Insert(0, new InputItemInfo("*", "Select All", ItemInfo.All(i => i.IsChecked)));

            foreach (InputItemInfo info in ItemInfo)
            {
                TagBuilder builder = new TagBuilder("input");
                if (info.IsChecked) builder.MergeAttribute("checked", "checked");
                builder.MergeAttribute("type", "checkbox");
                builder.MergeAttribute("value", info.Value);
                builder.MergeAttribute("name", name);
                builder.InnerHtml = info.DisplayText;
                sb.Append(builder.ToString(TagRenderMode.Normal));
                sb.Append("<br />");
            }

            return new HtmlString(sb.ToString());
        }

我能在我的意见,用这个也控制器得到的值,如下所示:

I was able to use this in my views and also get the values in the controller as shown below:

@model List<AppTest.Models.InputExtensionsViewModel>

@{
    ViewBag.Title = "Check";
}

<h2>Check</h2>

@using (Html.BeginForm())
{
    <table border="0" style="border:0px;">
        <tr>
            <td valign="top">
                @Html.Partial("CheckBoxList", Model[0])
            </td>

        </tr>
    </table>

    <br />

    <input type="submit" value="Go" />
}

<div style="font-weight:bolder">
    @ViewData["data"]
</div>

控制器:

public ActionResult Check()
        {
            var model = new List<InputExtensionsViewModel>();

            var model1 = new InputExtensionsViewModel
            {
                Title = "Facilities",
                InputElementName = "facilities",
                InputElements = // a list
            };

            model.Add(model1);
            return View(model);
       }

    [HttpPost]
    public ActionResult Check(string[] facilities)
    {
               ...
    }

该模型是:

public class InputExtensionsViewModel
    {
        public string Title { get; set; }
        public string InputElementName { get; set; }
        public List<InputItemInfo> InputElements { get; set; }

        public void SetSelected(string[] items)
        {
            if (items == null)
                return;

            this.InputElements.ForEach(delegate(InputItemInfo info)
            {
                if (items.Contains(info.Value))
                    info.IsChecked = true;
            });
        }
    }

我的问题是,有没有通过,我可以绑定在InputExtensionsViewModel模型中的阵列项目属性的方法吗?如果我只添加属性叫做设施视图模型,它不能自动约束,我可以理解为什么,因为我不是在我看来绑定。但我似乎不能想办法通过它我可以做到这一点。

My question is, is there a way by which I could bind the array items to a property in the InputExtensionsViewModel model? If I just add a property called facilities to the view model, it's not bound automatically and I can understand why, as I am not binding that in my view. But I cannot seem to think of a way by which I could do that.

这个复选框列表是一个用户控件,我只是想避免过多的String []我的操作方法数组。

This check box list is a user control and I just wanted to avoid having too many string[] array for my action methods.

- 好吧,我能做到这一点,当我现在尝试。不知道为什么它没有工作之前。

2)而且,我被检查的替代品,并发现了这个答案在SO:

2) And, I was checking for alternatives and found out this answer in SO:

的CheckBoxList在MVC3.0

和我能够复制这一点,但我的问题是,如何将标签绑定到该复选框?我的标签是动态的,模型的一部分​​,因此不能硬codeD。我试图用一个Html.LabelFor,但没有奏效。在编辑器中的模板,如果我只是@ Model.Text,它不会工作和后回作为后会丢失其不绑定到一个属性。

And I was able to replicate this but my question is, how do i bind a label to this checkbox? My labels are dynamic and part of the model and so cannot be hard-coded. I was trying to use a Html.LabelFor but that didn't work. In the editor template, if I just @Model.Text, it won't work and will be lost after a post-back as its not bound to a property

我用Google搜索,发现建议创建HTML佣工这是我先前做的(我的第一问题是关于这一点)。

I googled and found suggestions to create HTML helpers which is what I did earlier (my 1st question is about that).

请让我知道,如果事情还不清楚。我会详细说明。任何输入AP preciated!

Please let me know if something is unclear. I could elaborate. Any input is appreciated!

在此先感谢!

推荐答案

嗯,我找到了解决方案!

Ah, I found the solutions!

1)正如我在编辑说明 - 添加一个属性具有类似名称的模型,并在[HttpPost]启用操作方法,使用它工作正常。猜猜我错过了getter和setter方法​​最后一次。

1) As indicated in my edit - adding a property with a similar name to a model and using it in the [HttpPost] enabled action method works fine. Guess last time I missed the getter and setters.

2)对于这一点,在MyViewModel编辑模板,我们只需要添加这个(**和**,不用说,去除**):

2) For this, in the editor template for MyViewModel, we just need to add this (** and **, needless to say, remove **!):

@model AppName.Models.MyViewModel
@Html.HiddenFor(x => x.Id)           
@Html.CheckBoxFor(x => x.IsChecked) **@Model.Text
@Html.HiddenFor(x => x.Text)**

编辑:

我已经改变了这个模板做多。现在有一个标签控制和如下图所​​示它关联到jQuery的通过的复选框。

I have changed this template to do more. Now there is a label control and it is associated to the checkboxes through jquery as shown below.

@model EncorPlusTest.Infrastructure.InputItemInfo

@Html.HiddenFor(model => model.Value)
@Html.CheckBoxFor(model => model.IsChecked) <label for="">@Model.Text</label>
@Html.HiddenFor(model => model.Text)
<br />

然后在jQuery的:

Then in jquery:

$('input:checkbox').each(function () {
        var lbl = $(this).next('input:hidden').next('label');
        var forID = $(this).attr('id');
        $(lbl).attr('for', forID);
    });

希望其对他人有帮助!

Hope its helpful for others!

这篇关于ASP.Net MVC 3 - 的CheckBoxList - 需要一些建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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