English 中文(简体)
习俗范围数据说明无效(打破页面)
原标题:Date of birth custom range data annotation not working (breaking the page)

我有一个定制的数据注释验证器 来验证出生日期 从今天起150年

以下是我自定义的数据注释:

public class DateOfBirthRange : RangeAttribute
{
    public DateOfBirthRange()
        : base(typeof(DateTime), DateTime.Now.AddYears(-150).ToShortDateString(), DateTime.Now.ToShortDateString()) { }
}

像这样使用它:

[Required(ErrorMessage = "BirthDate is required.")]
[DisplayName("Birth Date")]
[DateOfBirthRange(ErrorMessage = "BirthDate must be between {1:M/d/yyyy} and {2:M/d/yyyy}")]
public DateTime BirthDate { get; set; }

这是一个非常奇怪的问题,因为我回来的错误与数据说明无关。

<%: Html.DropDownListFor(m => m.JuniorSenior, (IEnumerable<SelectListItem>)ViewData["seniority"], new { @class = "input-small" })%>

ERROR: 带有关键 Junior Senior 键的 ViewData 项是类型系统。 string 但必须是类型 IEnumber 。

此错误是奇怪的, 因为代码的这部分功能完全正常 。 < enger> 是什么也使这个错误变得奇怪 : 此错误仅在输入日期在今天150年之前时才发生 。 因此该错误只在出生验证日期失效时才发生 。 由于我正在调试, 我注意到, 一旦我删除了自定义的数据注释, 一切正常, 没有出错 。

因此,我假设问题就在我的数据注释中。

这是我的控制器代码 以防你想知道我想做什么

[HttpPost]
        public ActionResult Save(PatientModel patModel, FormCollection values)
        {
            // remove white space form text box fields
            patModel.FirstName = values["FirstName"].Trim();
            patModel.LastName = values["LastName"].Trim();
            patModel.Initials = values["Initials"].Trim();
            patModel.StreetAddress1 = values["StreetAddress1"].Trim();
            patModel.StreetAddress2 = values["StreetAddress2"].Trim();
            patModel.PostalCode = values["PostalCode"].Trim();

            if (ModelState.IsValid)
            {
                try
                {
                    // Pull the long form of the gender into the model.
                    if (!String.IsNullOrEmpty(values["genders"]))
                    {
                        patModel.Gender = values["genders"];
                    }
                    // Profile is valid, save it.
                    if (_Service.SaveProfile(Session["username"].ToString(), Session["password"].ToString(), patModel))
                        ViewData["SaveProfile"] = true;
                    else
                        ViewData["SaveProfile"] = false;
                }
                catch (Exception ex)
                {
                    logger.Error("Function Name: Save  Message: " + ex.Message + "");
                }
            }
            IntializeSelectLists(patModel);
            if (patModel.Title == "Select")
                patModel.Title = "";
            if (patModel.JuniorSenior == "Select")
                patModel.JuniorSenior = "";
            return View("Index", patModel);
        }

My Intialize 选择列表函数 :

public void IntializeSelectLists(PatientModel pm)
        {
            seniority = new[] { "Select", "Jr.", "Sr." };
            List<SelectListItem> JuniorSenior = new List<SelectListItem>();
            foreach (string item in seniority)
            {
                SelectListItem alb = new SelectListItem { Text = item, Value = item };
                JuniorSenior.Add(alb);
            }
            ViewData["seniority"] = JuniorSenior;
        }

任何帮助都将不胜感激。

最佳回答

撞到头后,我终于找到了问题所在

在我的 IntializeSectLists () I was producting the gles list 就在 JuniorSenior 列表之前。 事实证明, pm. Gender 正在返回无效, 因而造成了一个例外。

m.JuniorSenior 坠毁的原因是,我在 m.Gender 之前展示了这一点。因此,m.JuniorSenior 始终是无效的,因为代码没有到达它嵌入 m.JuniorSenior 的点。

patModel.Gender = values["genders"].Trim();

我通过在 Save () 中添加这个词来解决问题。

感谢所有花时间帮我的人:

问题回答

暂无回答




相关问题
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. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签