ModelState.AddModelError is not being displayed inside my view

后端 未结 3 1456
鱼传尺愫
鱼传尺愫 2020-12-13 08:50

I have the following view,, which create 10 ajax.beginform ,, But the problem that i am facing is that incase an error occurs during the creation of the object then the Mode

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 09:11


    I would urge you to change your try{ } catch(){ }

    And first check if there exists a visit for the given id and if so simply returns the model with the added model error

        if (visitExists)
        {
             ModelState.AddModelError("CustomError", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
             return View(vlr);    
        }
        //Other code here
    

    Change your AddModelError To

    ModelState.AddModelError("CustomError", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
    

    And in your view simply add a

    @Html.ValidationMessage("CustomError")
    

    Then when you return your model the error will be shown where you have placed the @Html.ValidationMessage ...

提交回复
热议问题