English 中文(简体)
在伙伴关系中,对一份清单进行验证。 NET MVC 3
原标题:Validating a List<string> in ASP.NET MVC 3

我有一页“我们”的网页,我有一份问答清单。

public List<string> Questions = new List<string>()
                                    {
                                        "What browser are you using?",
                                        "What version of the browser are you using?",
                                        "And so on and so forth..."
                                    };

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

接着,我也认为:

           @foreach (var question in Model.Questions)
            {
                <dt>
                    <dd width="240" height="25">@Html.Label(question)</dd>

                    <dd width="240" height="80">
                        @Html.ValidationMessageFor(model => model.Answers[Model.Index])
                        @Html.TextAreaFor(model => model.Answers[Model.Index], new { @class = "uniform", @id = "text" })
                      </dd>
                </dt>
                Model.Index += 1;
            }

Before I go about writing a custom validator, is there a way I can validate each string in that list of answers similar to the way I would validate a string field?:

[StringLength(100)]

预告!

问题回答

Your list of questions should be another model contained within your Contact Us page model so you can then assign those attributes to that model. This way you ll get the validation on each item.

另一种办法是检查控制器法的长度(当然在此没有客户确认)和使用示范国家。 AddError()在鉴定失败时人工添加错误。

第三个选择(保留方再次)是,在你的模型中实施可核查目标,以人工核对这些价值。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签