开发者

Validation textbox in MVC

开发者 https://www.devze.com 2022-12-22 06:52 出处:网络
I have the following code and I don\'t know were is the mistake: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(factura fac)

I have the following code and I don't know were is the mistake:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(factura fac)
    {
        if (fac.numar>0)
            ModelState.AddModelError("numar", "Numar este invalid .");
        if (fac.开发者_如何学Pythonserie.Trim().Length == 0)
            ModelState.AddModelError("serie", "Serie invalida");

        if (!ModelState.IsValid) return View("Create", fac);

    }

Here I try validate an textbox "serie" and I got the following error

Object reference not set to an instance of an object.

Thank you


First, could you please re-format to make easier to read? ie put all the code in a block?

Next, debug and check these expressions to see if they're null:

  • fac.serie
  • fac

It looks like either being null could throw this exception. It's probably the latter. If appropriate, wrap in a guard condition to check if it is null before evaluating.


Check string.IsNullOrEmpty(fac.serie) or string.IsNullOrWhitespace(fac.serie)

Most likely serie is null.

0

精彩评论

暂无评论...
验证码 换一张
取 消