문제

I have a scenario like i need to display error message which is coming from DB on Edit [GET] request.

I know this can be done if request type is of [POST], but how can we do this in [GET] request.

Same Code:

    [HttpGet]
    public ActionResult Edit(Int64 ID)
      {
         tblSample1 model = GetData(ID);
         ViewData.ModelState.AddModelError(model.Username, "Invalid Username provided.");
         return View("~/Views/Sample1/_Edit.cshtml", model);
      }

[HttpPost] public ActionResult Edit(tblSample1 model) { if (ModelState.IsValid) { ...... ...... } }

도움이 되었습니까?

해결책

This should still work. The first argument to AddModelError is the key. You're passing it the value of the property Username.. which won't work. What you want is to pass the property name as the key:

ModelState.AddModelError("Username", "Invalid Username provided.");
//                       ^^^^^^^^^^ Username property of model

Of course this must be coupled with a ValidationSummary or ValidationMessage in your view.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top